angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

webpack ng serve runs out of memory

Open slubowsky opened this issue 10 years ago • 107 comments

Please provide us with the following information:

  1. OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Windows 10
  2. Versions. Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: angular-cli: 1.0.0-beta.11-webpack.2 node: 6.3.1 os: win32 x64
  3. Repro steps. Was this an app that wasn't created using the CLI? What change did you do on your code? etc. Keep ng serve running and serving a cli built app while coding for some time (a few hours?)
  4. The log given by the failure. Normally this include a stack trace and some more information.
...
 94% asset optimization
<--- Last few GCs --->

12936118 ms: Mark-sweep 1360.3 (1435.0) -> 1356.1 (1435.0) MB, 959.3 / 0 ms [all
ocation failure] [GC in old space requested].
12936973 ms: Mark-sweep 1356.1 (1435.0) -> 1356.0 (1435.0) MB, 854.9 / 0 ms [all
ocation failure] [GC in old space requested].
12938085 ms: Mark-sweep 1356.0 (1435.0) -> 1356.0 (1435.0) MB, 1112.2 / 0 ms [la
st resort gc].
12939012 ms: Mark-sweep 1356.0 (1435.0) -> 1355.8 (1435.0) MB, 926.7 / 0 ms [las
t resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 000001F1975C9E51 <JS Object>
    1: replace [native string.js:~129] [pc=0000018EE86B74E5] (this=000000EF14282
331 <String[61]: D:/it/node_modules/@angular/common/src/pipes/common_pipes.js>,
N=000000EF140824C9 <JS RegExp>,O=00000135F03D7F21 <String[10]: !(webpack)>)
    2: shorten [D:\it\node_modules\webpack\lib\RequestShortener.js:~41] [pc=000
0018EE4290DA2] (this=0000020E88D58019 <a RequestShortener with map 00000313C3C4.
..

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memo
ry
  1. Mention any other details that might be useful.

Only happened once so far but thought it worth mentioning


Thanks! We'll be in touch soon.

slubowsky avatar Aug 11 '16 20:08 slubowsky

i have noticed the memory consumption decreases if typescript code is optimised - like removing unused imports, unused methods, unused variables. also i noticed variables should be declared as "let" or "const" not var.. and last but not the least using tslint plugin i removed most of the code issues and my angular build memory consumption came down from 4gb to 2 gb. Earlier my angular build was failing with 3gb and a minimum 4gb was required to build the prj but after doing the process mentioned above it started building with 2gb ram. Forgot to mention - i am using idea intellij ultimate which shows many such issues in the angular code.

sonirahul avatar Jul 18 '17 04:07 sonirahul

@danishin Sorry no - but you could start deleting more non-trival stuff like templates with pipes, ng-content etc, - just delete and delete until it builds and than put things back in until you find out what is responsible

xnnkmd avatar Jul 18 '17 08:07 xnnkmd

@danishin BTW: How big is your mid-sized project measured in #components, #files, #source-code-lines ?

xnnkmd avatar Jul 18 '17 09:07 xnnkmd

@xnnkmd It's about 10K LOC of .ts files. Not a big project at all. I realized that in tsconfig.json, module is set to commonjs instead of es2015 as explained in here.

Will try after making the changes. I have a feeling that what @tariknz said https://github.com/angular/angular-cli/issues/1652#issuecomment-315955861 is the root cause of this. It seems that during the compilation, there was an error and memory leak because of it.

danishin avatar Jul 18 '17 13:07 danishin

Just as a heads up. It turns out when I use YARN instead of NPM to RESTORE packages ng build completes successfully.

I am using

ng build --env=prod --aot

When I restore packages using npm install -- same error from this issue (out of memory) When I restore packages using yarn install -- no error - success

brunobertechini avatar Aug 10 '17 16:08 brunobertechini

Thanks @basherr! But instead of running script from command line. Try running build script in package json by the following script:

"scripts": {
   "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod"
}

then run "npm run build-prod"

RicTheThird avatar Aug 14 '17 03:08 RicTheThird

@RicTheThird and @basherr - worked for me, thanks.

chronicbint avatar Aug 14 '17 08:08 chronicbint

@RicTheThird - thx. works for me. o/

By the way, i preferred changing the start script rather than adding a new one.

scharlo avatar Aug 22 '17 14:08 scharlo

"scripts": { "ng-custom-serve": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng serve" } as suggested by @RicTheThird this is what i have done to resolve,having ng serve as well,but will this hold good?,what if the application grows?Can we hope on this and go ahead?Any help would be great?

deepakanuraags avatar Aug 25 '17 16:08 deepakanuraags

In my case problem was solved after force adding @ngtools/webpack package to dependencies. And using --max_old_space_size is was not required.

igorrius avatar Sep 28 '17 07:09 igorrius

I've tried building project with latest webpack till date(i.e. @3.8.1) and it solved the similar issue for me.

muhammadsaaeed avatar Oct 30 '17 12:10 muhammadsaaeed

npm uninstall -g @angular/cli and reinstall @angular/[email protected] solved my problem

finalxcode avatar Nov 07 '17 01:11 finalxcode

I had experienced the issue FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory with 'ng build --prod'. I have solved the problem by using node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

You can add also in package.json as

"scripts": {
    "prod": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng  build --prod",
}

npm run prod

rejayi-experion avatar Nov 10 '17 07:11 rejayi-experion

Hi, in my case, none of the solutions above worked for me. I commented here about my execution crashing when it goes above 500 mb I leave you here the link in case that thread is useful to someone

MartinFreire avatar Nov 21 '17 16:11 MartinFreire

Thanks @rejayi-experion Modifying NG.cmd didn't work but just invoking it manually as you suggested did.

nhustak avatar Nov 28 '17 00:11 nhustak

I mean, is there a reasonable explanation why it takes minutes to compile HTML, TS, and (S)CSS?

ghost avatar Nov 28 '17 16:11 ghost

Anyone have a more definitive solution for this? Anyone comment that @TheLarkInn is working in the issue. There is another thread open to this issue?

JBGoldberg avatar Dec 11 '17 18:12 JBGoldberg

node --max_old_space_size=2048 ./node_modules/.bin/ng Worked for me thanks

isahyarima avatar Dec 29 '17 13:12 isahyarima

For anyone that might be trying to debug why all of a sudden the build process started running out of memory, I was able to track the issue down to the following code in my project.

Before change (out of memory exception):

  handleResponse(response: Response) {
    if (response.status == 403) {
      this.authenticationService.expireAuthToken();
      return Observable.throw('Session Timeout');
    }

    let json = response.json();

    return Observable.of({
      success: false,
      errors: json
    });
  }

After change (no memory issues):

  handleResponse(response: Response): Observable<any> { // <--- ADDED return type
    if (response.status == 403) {
      this.authenticationService.expireAuthToken();
      return Observable.throw('Session Timeout');
    }

    let json = response.json();

    return Observable.of({
      success: false,
      errors: json
    });
  }

Using the code of the before change, every time that method was called, it caused more and more memory being used during ng serve or ng build.

Not sure why specifying the return type fixes the issue, but went from ng build using ~3gb of memory down to ~700mb.

thecheeto avatar Feb 08 '18 20:02 thecheeto

@thecheeto Did you add this modification to every function within your project? And even if it returns void?

vitor-belim avatar Feb 23 '18 17:02 vitor-belim