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

What version of NodeJs are you running here? If < 8.9, would you mind testing and reprofiling. There is significant work done in V8 with garbage collection that surfaced in that version of node that we collaborated with the V8 team on.

TheLarkInn avatar Jan 04 '18 20:01 TheLarkInn

@TheLarkInn updated from v8.4.0 to v9.3.0. Profiled first 12 minutes of RemoveParentModulesPlugin execution:

screenshot 2018-01-05 16 09 33

CPU-20180105T160658.cpuprofile.zip

pleerock avatar Jan 05 '18 11:01 pleerock

@JaapMosselman the chunk asset optimization in prod builds is when module concatenations and Uglification happen. This are the optimizations that reduce your bundle size by analyzing code and dropping unused one.

It is slow by it's very nature, but https://github.com/angular/angular-cli/pull/8668 (merged but waiting for 1.7 to be released) will help by running Uglify in parallel.

@pleerock I've been looking at Webpack 4 and thought maybe it could help since there are a lot of performance improvements there. The plugin in question has suffered some changes:

  • in [email protected]: https://github.com/webpack/webpack/blob/b545b519b2024e3f8be3041385bd326bf5d24449/lib/optimize/RemoveParentModulesPlugin.js
  • in [email protected]: https://github.com/webpack/webpack/blob/9334fd8547831ded7f11e05319616a72ea0b4698/lib/optimize/RemoveParentModulesPlugin.js

It looks like there's a fair bit of optimizations changes there. But I'm not sure if the bottleneck has changed.

There's two places where time is being spent on your CPU profile:

  • a has call inside the hasModule function: image
  • a filter call inside the hasModule function: image

This function hasn't changed though so I don't think it would speed up your case. I'm going to open an issue in webpack about this.

EDIT: opened as https://github.com/webpack/webpack/issues/6248

filipesilva avatar Jan 05 '18 14:01 filipesilva

Started getting this issue last month, did try so many workarounds and got it working after upgrading to angular-cli 1.6.2. But the build started taking about 45 minutes, made it difficult to push new builds. But at least it was working. Now getting the issue again, and this time, it only works occasionally. And when it works it still takes about an hour. I am guessing the issue started occurring after the codebase grew above some threshold, before this, the build used to take ~10 mins. Is there a way to get over the frequent build failures. This is the output I am getting:

92% chunk asset optimization<--- Last few GCs --->

[34132:0000025213C0DB90]  2282774 ms: Mark-sweep 1401.9 (1713.9) -> 1401.9 (1699.9) MB, 1637.6 / 0.0 ms  last resort GC in old space requested


<--- JS stacktrace --->

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

Security context: 0000037423C25EE1 <JSObject>
    2: visit [0000033FF8602311 <undefined>:7667] [bytecode=0000002651BE0769 offset=620](this=0000038789453039 <TreeWalker map = 0000020B4AB1BD21>,node=000001D6E5AAD121 <AST_VarDef map = 000002C8A2BBA649>,descend=0000001D6E073821 <JSFunction (sfi = 000003D9CF111AF9)>)
    4: _visit [0000033FF8602311 <undefined>:1512] [bytecode=000003D9CF10CF99 offset=57](this=0000038789453039 <TreeWalker map = ...

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

Angular Version: 4.2.4 Angular-cli Version: 1.6.2 TypeScript Version: 2.3.4 Node Version: 8.9.3 npm Version: 5.5.1

This are the number of individual files:

      2 gif
      1 gitkeep
    252 html
      1 ico
      5 jpg
      1 js
      6 json
    110 png
    390 scss
    509 ts

Any guidance will be much appreciated. Stuck at this no build situation for the last 24 hours 😕

sabeersulaiman avatar Jan 08 '18 16:01 sabeersulaiman

@sabeersulaiman have you tried setting a npm script for increasing the memory available? I mentioned it in https://github.com/angular/angular-cli/issues/5618#issuecomment-348225508.

If you still have very long builds (10+ minutes) and have a repro available, I'd love to look into it.

filipesilva avatar Jan 08 '18 17:01 filipesilva

@filipesilva that did the trick. I tried it but I think I mistakenly tried --max-old-space-size instead of --max_old_space_size. It works fine now and only takes ~4 min. And about ~8 min on CI server. Thanks.

sabeersulaiman avatar Jan 09 '18 05:01 sabeersulaiman

I have made some research and according to this both --max-old-space-size and --max_old_space_size syntax are valid. Here is a link in nodel documentation about --max-old-space-size. Also I found some people complain that first approach is not working, but obliviously both should work.

pleerock avatar Jan 12 '18 08:01 pleerock

@filipesilva Im able to do build using webpack3, latest angularcli and it takes 1 hour with node 8 as we discussed.

Now, Im trying to build production version using build --prod and having a memory error on 92%:

node --max_old_space_size=16384 ./node_modules/@angular/cli/bin/ng "build" "--prod"

 10% building modules 5/6 modules 1 active ...du.dev/src/assets/css/styles-prod.cssNode#mo  92% chunk asset optimization                                                      <--- Last few GCs --->

[633:0x102804600]  6440993 ms: Scavenge 3626.0 (4651.3) -> 3611.8 (4651.3) MB, 28.9 / 0.0 ms  allocation failure 
[633:0x102804600]  6441072 ms: Scavenge 3627.1 (4651.3) -> 3612.5 (4651.3) MB, 26.6 / 0.0 ms  allocation failure 
[633:0x102804600]  6452624 ms: Mark-sweep 4189.7 (4899.4) -> 3245.2 (4747.3) MB, 344.1 / 0.0 ms  (+ 3081.6 ms in 990 steps since start of marking, biggest step 6.3 ms, walltime since start of marking 4825 ms) finalize incremental marking via stack guard G

<--- JS stacktrace --->

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

Security context: 0x27d30fb25ee1 <JSObject>
    1: flatten_args(aka flatten_args) [0x27d3efb02311 <undefined>:~11681] [pc=0x372b5a5bba0e](this=0x27d3efb02311 <undefined>,fn=0x27d3b7e0fe01 <AST_Function map = 0x27d444b33ab1>,scope=0x27d3b7e0f631 <AST_Function map = 0x27d411240269>)
    3: /* anonymous */(aka /* anonymous */) [0x27d3efb02311 <undefined>:11627] [bytecode=0x27d350f41949 offset=5456](this=0x27d3efb02311 <undefined>,self=0x27d4...

FATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/local/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
 4: v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::EnsureCapacity(v8::internal::Handle<v8::internal::StringTable>, int, v8::internal::PretenureFlag) [/usr/local/bin/node]
 5: v8::internal::StringTable::LookupKey(v8::internal::Isolate*, v8::internal::StringTableKey*) [/usr/local/bin/node]
 6: v8::internal::StringTable::LookupString(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>) [/usr/local/bin/node]
 7: v8::internal::LookupIterator::LookupIterator(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>, v8::internal::LookupIterator::Configuration) [/usr/local/bin/node]
 8: v8::internal::LookupIterator::PropertyOrElement(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, bool*, v8::internal::LookupIterator::Configuration) [/usr/local/bin/node]
 9: v8::internal::Runtime_KeyedGetProperty(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
10: 0x372b54a8463d
11: 0x372b5a5bba0e

pleerock avatar Jan 12 '18 10:01 pleerock

If you have the time, can you try with CLI 1.7.0-beta.0?

clydin avatar Jan 12 '18 12:01 clydin

Looks like something got better, build times reduced by 50% in my project + memory consumption as well (for single node process) , although I think I see more node processes being spawned. @clydin : what was fixed?

dragonflypl avatar Jan 12 '18 15:01 dragonflypl

@pleerock about --max-old-space-size and --max_old_space_size. node 8.9.3 on Windows gives me the following error while using --max-old-space-size

 $ npm run start

> [email protected] start C:\Projects\test
> node --max-old-space-size=8192 ./node_modules/.bin/ng serve

C:\Projects\test\node_modules\.bin\ng:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node --max-old-space-size=8192 ./node_modules/.bin/ng serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\sabee\AppData\Roaming\npm-cache\_logs\2018-01-13T04_47_40_099Z-debug.log

Is this supposed to happen or is it an issue with the way I configured it ?

sabeersulaiman avatar Jan 13 '18 04:01 sabeersulaiman

@filipesilva I was able to build site using webpack4 (with fixes applied by webpack team) + aot using your branch in 40 minutes. But for some reason index.html is not emitted (even with regular dev build)

pleerock avatar Jan 13 '18 18:01 pleerock

This error happen with cli 1.6.2

node --max_old_space_size=12288 ./node_modules/.bin/ng build --prod

use this command works for me

rainstormza avatar Jan 16 '18 02:01 rainstormza

updated to @angular/[email protected] but still the same error 'FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory'

Sarfarazsajjad avatar Jan 18 '18 20:01 Sarfarazsajjad

This started happening for us. How can this happen when I have locked down all my angular versions? Does the CLI not follow the proper versioning? Is there any fix for this?

rafiwardak2003 avatar Jan 19 '18 03:01 rafiwardak2003

@Sarfarazsajjad and @sabeersulaiman are you trying with 64 bit node or 32 bit node?

64 bit node with --max_old_space_size should build fine for large applications in --prod mode.

Satyadatla avatar Jan 19 '18 08:01 Satyadatla

I tried what @clydin said, using 1.7.0-beta.0 worked like a charm, it did cut my building time from over 30 minutes to 3 minutes. Thank you angular-cli team.

nadhir-falta avatar Jan 19 '18 14:01 nadhir-falta

I did also a test run with 1.7.0-beta.0. The build time dropped from 13 minutes to 6.5 minutes (on my not to fast build machine). On my dev machine it builds now in 3.6 minutes (with 1.6.0 it was 7.8 minutes). The size of the main bundling increased a little from 2653 to 2664 KB. I needed to add @angular-devkit/core as a dev dependency to my package, else it would not be installed automatically.

JaapMosselman avatar Jan 23 '18 07:01 JaapMosselman

Tested following wit cli 1.6.5

$ node --max_old_space_size=8192 node_modules/.bin/ng build --prod
Date: 2018-01-26T07:31:27.619Z
Hash: 50238294b6564c10ffb7
Time: 130002ms
chunk {0} polyfills.63eb1df6d53730045032.bundle.js (polyfills) 97.8 kB [initial] [rendered]
chunk {1} main.401dee7547b2af51d287.bundle.js (main) 1.57 MB [initial] [rendered]
chunk {2} styles.3f5700be7447a96e8808.bundle.css (styles) 50.2 kB [initial] [rendered]
chunk {3} inline.82501ecadee5847574b3.bundle.js (inline) 1.45 kB [entry] [rendered]

$ ng build --prod
Date: 2018-01-26T07:35:55.320Z
Hash: 50238294b6564c10ffb7
Time: 136006ms
chunk {0} polyfills.63eb1df6d53730045032.bundle.js (polyfills) 97.8 kB [initial] [rendered]
chunk {1} main.401dee7547b2af51d287.bundle.js (main) 1.57 MB [initial] [rendered]
chunk {2} styles.3f5700be7447a96e8808.bundle.css (styles) 50.2 kB [initial] [rendered]
chunk {3} inline.82501ecadee5847574b3.bundle.js (inline) 1.45 kB [entry] [rendered]

So 6 seconds ... not worth it ...

ghost avatar Jan 26 '18 07:01 ghost

i had to move from angular-cli 1.5.0 to 1.6.7 to resolve my build memory bug (without setting --max_old_space_size)

mcherb avatar Feb 07 '18 14:02 mcherb