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

AOT build fails due to "JavaScript heap out of memory"

Open ghost opened this issue 9 years ago • 427 comments

Bug Report or Feature Request (mark with an x)

- [X ] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.0.0
node: 7.7.4
os: win32 x64
@angular/animations: 4.0.0
@angular/common: 4.0.0
@angular/compiler: 4.0.0
@angular/core: 4.0.0
@angular/flex-layout: 2.0.0-rc.1
@angular/forms: 4.0.0
@angular/http: 4.0.0
@angular/material: 2.0.0-beta.2
@angular/platform-browser: 4.0.0
@angular/platform-browser-dynamic: 4.0.0
@angular/router: 4.0.0
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.0

Repro steps.

ng build --prod --aot

The log given by the failure.

/node_modules/@angular/material/aut 92% chunk asset optimization
<--- Last few GCs --->

[6328:0000016AB4D09F00]  1775597 ms: Mark-sweep 1411.2 (1513.5) -> 1411.0 (1513.5) MB, 1240.6 / 0.0 ms  last resort


<--- JS stacktrace --->

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

Security context: 0000034B0DD266A1 <JS Object>
    2: optimize [0000000FABC02311 <undefined>:5480] [pc=00000082575652C8](this=000000633199A279 <an AST_Call with map 0000014753FA0291>,compressor=00000226DBDECA61 <a Compressor with map 0000032B1EC8F829>)
    3: before [0000000FABC02311 <undefined>:5463] [pc=0000008258C1818D](this=00000226DBDECA61 <a Compressor with map 0000032B1EC8F829>,node=000000633199A279 <an AST_Call with map 0000014753...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Desired functionality.

I would like to be able the app using AOT.

Mention any other details that might be useful.

I have tried changing ng.cmd to this:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
)

and ngc.cmd to:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
)

I have 8GB swap file. The build fails after 20-25 minutes standing on 92% progress.

ghost avatar Mar 24 '17 09:03 ghost

@notsonotso an ng team member told me this has to do with poor recursion handling in @ngtools/webpack, i'm curious what webpack's faults are?

zackschuster avatar Oct 23 '17 19:10 zackschuster

@zackschuster its mere existence

ghost avatar Oct 23 '17 20:10 ghost

Started happening to me using macOS 10.13 On Windows it builds fine, both machines using the same configuration and have 16 gigs of ram.

MrBlaise avatar Oct 26 '17 09:10 MrBlaise

I went back to version 1.2.x and didn't have any exceptions there.

1.3.x and 1.4.x all gave the memory exception.

MbuelensAM avatar Oct 26 '17 10:10 MbuelensAM

@filipesilva Is there any news regarding to this issue? I'm curious why a semi-large project needs more than 2 gigs to build. I am not familiar with the inner workings of the compilation process, but it seems rather high to me.

EDIT:

using the --build-optimizer flag causes the issue, it was working fine, but I recently upgraded the cli and I see that it uses a newer build-optimizer (0.0.31), maybe memory leak on the optimizer?

MrBlaise avatar Oct 26 '17 11:10 MrBlaise

@MrBlaisee you mean 20* Go

!!

elvisbegovic avatar Oct 26 '17 11:10 elvisbegovic

try ng build --aot --sourcemaps=false , it seems there is problem with sourcemaps.

GenweiWu avatar Oct 28 '17 07:10 GenweiWu

Lol with or without sourcemap it depend how long node is working! Hope this will be fixed one day

elvisbegovic avatar Oct 28 '17 21:10 elvisbegovic

I am using 6.9.4 Nodejs and Angular 4.5, Angular-cli 1.4.6 but no lucks

We need hacks above.

tupham avatar Oct 31 '17 11:10 tupham

Got an issue resolved on one of the machine using below:

Try this out: In "ProjectDirectory\node_modules\.bin" modify webpack.cmd to:

@if EXIST "%~dp0\node.exe" ( "%~dp0\node.exe --max_old_space_size=4096 " "%~dp0..\webpack\bin\webpack.js" %* ) ELSE ( @SETLOCAL @set PATHEXT=%PATHEXT:;.JS;=;% node --max_old_space_size=4096 "%~dp0..\webpack\bin\webpack.js" %* )

and try to build. Basically, its going to use --max_old_space_size=4096 with node webpack build.

See if that helps.

muhammadsaaeed avatar Oct 31 '17 11:10 muhammadsaaeed

Angular 5 and angular-cli 1.5. Have problems --aot

95% emitting
<--- Last few GCs --->

  152742 ms: Mark-sweep 1261.4 (1434.1) -> 1261.4 (1434.1) MB, 471.1 / 0.0 ms [allocation failure] [scavenge might not succeed].
  153236 ms: Mark-sweep 1261.4 (1434.1) -> 1261.4 (1434.1) MB, 493.7 / 0.0 ms [allocation failure] [scavenge might not succeed].
  153757 ms: Mark-sweep 1261.4 (1434.1) -> 1268.2 (1418.1) MB, 520.5 / 0.0 ms [last resort gc].
  154333 ms: Mark-sweep 1268.2 (1418.1) -> 1275.0 (1418.1) MB, 575.8 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 000003AFBC7CFB61 <JS Object>
    1: DoJoin(aka DoJoin) [native array.js:~129] [pc=00000055CD16A7F7] (this=000003AFBC704381 <undefined>,w=0000011B90EE0C21 <JS Array[539]>,x=539,N=000003AFBC
7043C1 <true>,J=000003AFBC7AE4E1 <String[1]:  >,I=000003AFBC7B46F1 <JS Function ConvertToString (SharedFunctionInfo 000003AFBC752DC9)>)
    2: Join(aka Join) [native array.js:180] [pc=00000055CD1D6A52] (this=000003AFBC704381 <undefined>...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

ar53n avatar Nov 02 '17 13:11 ar53n

Also hit this memory issue. Can be 'resolved' by using the build script node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod but hopefully this bug will be fixed soon. Why does it need over 2GB of memory?!

mbrookson avatar Nov 02 '17 14:11 mbrookson

Also facing the same issue currently, our app has over 50 components spread across 6 modules.

RicardoVaranda avatar Nov 03 '17 09:11 RicardoVaranda

We are experiencing the same issue... eclipsing 3GB of memory in a medium-large application. We have ~20 modules each with 5-8 components.

dpxxdp avatar Nov 03 '17 15:11 dpxxdp

Hey all, I'm seeing a lot of new reports of this happening after 1.5 is out. One of the changes we did for 1.5 was to use Build Optimizer by default on Angular version 5 projects. Build optimizer does indeed use a lot of memory (https://github.com/angular/devkit/issues/240).

Can you tell me if turning off build optimizer helps with your memory crashes? You can do it with --build-optimizer=false. Turning off sourcemaps can also help via --sourcemaps=false.

If these options don't help, can you post here the full failure log please, including command run?

filipesilva avatar Nov 04 '17 10:11 filipesilva

--build-optimizer=false helps in my case.

I have a large app (30 modules, 263 components).

With @angular: 4.4.6 ; @angular/cli: 1.4.9 I can get a --prod build with --max_old_space_size=6144 but with @angular: 5.0.0 ; @angular/cli: 1.5.0 I run out of memory even with --max_old_space_size=8192. I can get a build with --max_old_space_size=10240, but my CI does not support that configuration.

> node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod


<--- Last few GCs --->

  387148 ms: Mark-sweep 4983.1 (8230.5) -> 4987.2 (8230.5) MB, 860.4 / 0.0 ms [allocation failure] [scavenge might not succeed].
  387947 ms: Mark-sweep 4987.2 (8230.5) -> 4987.3 (8230.5) MB, 798.9 / 0.0 ms [allocation failure] [scavenge might not succeed].
  388816 ms: Mark-sweep 4987.3 (8230.5) -> 5002.0 (8214.5) MB, 869.3 / 0.0 ms [last resort gc].
  389619 ms: Mark-sweep 5002.0 (8214.5) -> 5017.0 (8214.5) MB, 802.0 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 0x2199619cfb39 <JS Object>
    1: DoJoin(aka DoJoin) [native array.js:~129] [pc=0x29bfc23b9949] (this=0x219961904381 <undefined>,w=0x3864fdc19d1 <JS Array[537]>,x=537,N=0x2199619043c1 <true>,J=0x2199619ae4b9 <String[1]:  >,I=0x2199619b46c9 <JS Function ConvertToString (SharedFunctionInfo 0x219961952dc9)>)
    2: Join(aka Join) [native array.js:180] [pc=0x29bfc2ccdb52] (this=0x219961904381 <undefined>,w=0x3864fdc19d1 <JS ...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/Users/*****/.nvm/versions/node/v6.11.0/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/Users/*****/.nvm/versions/node/v6.11.0/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/Users/*****/.nvm/versions/node/v6.11.0/bin/node]
 4: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/Users/*****/.nvm/versions/node/v6.11.0/bin/node]
 5: v8::internal::Runtime_StringBuilderJoin(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/*****/.nvm/versions/node/v6.11.0/bin/node]
 6: 0x29bfae0092a7


Angular CLI: 1.5.0
Node: 6.11.0
OS: darwin x64
Angular: 5.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.5.0
@angular-devkit/build-optimizer: 0.0.32
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.35
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0
@schematics/angular: 0.1.0
typescript: 2.4.2
webpack: 3.8.1

However, with --build-optimizer=false I can build with --max_old_space_size=6144

> node --max_old_space_size=6144 ./node_modules/@angular/cli/bin/ng build --prod  --build-optimizer=false

Date: 2017-11-04T23:08:35.111Z
Hash: afdf23d865ad76d6c3a7
Time: 252357ms
chunk {0} 0.5fdb723fb2afdf8bc41a.chunk.js (common) 657 kB  [rendered]
chunk {1} 1.9d7656022b2d3d139dc3.chunk.js () 9.23 MB  [rendered]
chunk {2} 2.0fd8e1693141dc5c5223.chunk.js () 3.15 MB  [rendered]
chunk {3} main.46f18abd9489952e107d.bundle.js (main) 3.43 MB [initial] [rendered]
chunk {4} polyfills.0acd42e0b1fa025cc794.bundle.js (polyfills) 222 kB [initial] [rendered]
chunk {5} styles.27285cfcd50901377a47.bundle.css (styles) 583 kB [initial] [rendered]
chunk {6} vendor.6b76d39b8f45ef045486.bundle.js (vendor) 1.1 MB [initial] [rendered]
chunk {7} inline.e8ad567f621efe38acfe.bundle.js (inline) 1.52 kB [entry] [rendered]

mscahill avatar Nov 04 '17 23:11 mscahill

Tests with angular-cli 1.5

ng build --prod build-optimizer=false --sourcemaps=false (I canceled after 12 minutes running)

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --prod build-optimizer=false --sourcemaps=false (work after 4,2 minutes)

williangd avatar Nov 06 '17 11:11 williangd

@filipesilva, @hansl, Before anything, thanks so much for this project- Angular has been great for our team. We love the framework and we love the CLI so thanks for all your hard work.

This memory issue seems like it is becoming a real problem. I'm looking for some messaging from the Angular team around expectations here, and I'd be happy to give whatever data is necessary to help move it forward.

Our team is an enterprise team with a medium-large app. We ran into this memory issue a while back, and upped the max_old_space_size to 1GB and then later 2GB. Each time it solved our problem for a few months. But as the app continues to grow we've run into it again now and we can't do this forever. Our toolchain uses Bitbucket pipelines, which like many other CI's maxes out available memory (3GB is our max given our other needs), so we're beginning to run up against a wall.

I've been taking some benchmarks with our app, it looks like you're correct in your post above: 1.5.0, with --build-optimizer=false uses equivalent memory to 1.4.5.

Our app has ~180 components and ~30 services across ~22 modules. I'd like to think it is constructed in a reasonably smart manner, following best practices and general clean code design. The build --aot --build-optimizer=false --sourcemaps=false for this project using 1.5.0 now takes about 2925 GB of memory.

Two general questions for you and others, about (1) expectations and (2) short-term alleviation:

(1) Is Angular built for enterprise development? If so, this issue seems to be of the highest priority and the community deserves more frequent and clear messaging on this. If not, your users need to know this because lots of decisions across hundreds of companies ride on that.

(2) Are there any recommendations from the Angular team about ways to alleviate this issue?

  • Should we split out our modules into "pre-compiled" libraries?
  • Is memory usage particularly worse for high Component counts, high Service counts, etc?
  • Other flags that help trade memory for build-time?

Again, I'd be happy to test any theories/provide data about our large application to help get to the bottom of this, and again, thanks for all the hard work you've done on this.

dpxxdp avatar Nov 06 '17 11:11 dpxxdp

In our case, the build optimizer nor the sourcemaps flags were effective. Downgrading to @angular/cli 1.4.9 fixed it.

Worked without either flag set false:

ng build -prod

@angular/cli: 1.4.9
node: 6.9.2
os: linux x64
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.9
@angular/compiler-cli: 4.4.6
typescript: 2.3.4

DIDN'T Work even with both flags set false:

ng build -prod --build-optimizer=false --sourcemaps=false
Angular CLI: 1.5.0
Node: 6.9.2
OS: linux x64
Angular: 4.4.6
... common, compiler, compiler-cli, core, forms, http
... platform-browser, platform-browser-dynamic, router
... tsc-wrapped

@angular/cli: 1.5.0
 @angular-devkit/build-optimizer: 0.0.32
 @angular-devkit/core: 0.0.20
 @angular-devkit/schematics: 0.0.35
 @ngtools/json-schema: 1.1.0
 @ngtools/webpack: 1.8.0
 @schematics/angular: 0.1.0
typescript: 2.3.4
webpack: 3.8.1

...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

We have not tried the hacks above on changing the old space memory... our application isn't apparently as big as some of the others on this thread, but yet fails on cli 1.5.0.

jason-d-hobbs avatar Nov 06 '17 21:11 jason-d-hobbs

@filipesilva: --build-optimizer=false and --sourcemaps=false works for me with cli 1.5. Without them, I was getting "JavaScript heap out of memory". Thanks for your help!

danny-swimmer avatar Nov 07 '17 15:11 danny-swimmer