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

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.

For me, 1.7.0-beta.3 not work. Still get:

Security context: 0x3926bacfb39 <JS Object>
    2: write [/node_modules/typescript/lib/typescript.js:~9597] [pc=0x93487866c7f] (this=0x10feca1f0819 <an Object with map 0x98bc5b8c929>,s=0x39beab773a79 <String[2]: i4>)
    3: pipelineEmitExpression(aka pipelineEmitExpression) [/node_modules/typescript/lib/typescript.js:~69005] [pc=0x93487876ff7] (this=0x3926ba04381 <undef...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
Angular CLI: 1.7.0-beta.3
Node: 6.10.1
OS: darwin x64
Angular: 5.2.4
... common, compiler, compiler-cli, core, forms, http
... platform-browser, platform-browser-dynamic, router

@angular/cli: 1.7.0-beta.3
@angular-devkit/build-optimizer: 0.2.0
@angular-devkit/core: 0.2.0
@angular-devkit/schematics: 0.2.0
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.10.0-beta.3
@schematics/angular: 0.2.0
@schematics/package-update: 0.2.0
typescript: 2.6.2
webpack: 3.10.0

smiera avatar Feb 12 '18 19:02 smiera

update global and local Angular Cli to 1.7.0-rc.0 work for me

update global Angular Cli

npm uninstall -g @angular/cli
npm cache clean --force
npm install -g @angular/[email protected]

update local Angular Cli npm install @angular/[email protected]

kanji-keraliya avatar Feb 15 '18 10:02 kanji-keraliya

I'd be careful with rc.0 - it corrupts some assets in aot builds e.g. fonts. I think it's been fixed but there hasn't been a release yet.

warmans avatar Feb 15 '18 12:02 warmans

Using @angular/[email protected] DID NOT fix the issue.

Workaround: "ng-high-mem": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng", as mentioned had to be used.

This is something which needs to be fixed guys.

PsyGik avatar Feb 15 '18 13:02 PsyGik

Just give it more memory. It is inevitable that the process consumes more and more memory as the project size grows. There's no way around that. The only thing that (probably) will fix this is https://bazel.build, but that's still quite far in the future.

So, the fix imho is that ng just adds some node flags by default and let's call it a day.

swftvsn avatar Feb 15 '18 14:02 swftvsn

It's not inevitable. It does point to a fundamental flaw in the build architecture. If we default the --max_old_space_size=8192 flag, how large of a project will this build until that value is too small?

kdubious avatar Feb 15 '18 15:02 kdubious

I'm just being practical, the build uses heavily webpack, node etc. and a lot of other dependencies that may not be trivial to rewrite. The change to bazel is coming, so how many man years should be put into this old build pipeline?

As long as the memory requirement is not outrageous, I'd like to vote the effort to be put into something else, as there are a lot of things that need attention. It's 2018, so I don't think 4 or 8 gigs of memory is that much.

I also don't think that many users would have noticed that the build uses 4 gigs of memory if that was the default.

That said, I'll shut up now ;)

swftvsn avatar Feb 15 '18 15:02 swftvsn

I think something is kind of messed up with the SASS processing which might be contributing to these issues (definitely for dev builds but I'm not 100% sure for aot).

Essentially there seems to be something weird happening with sass de-duplication. In my project I had a common sass file with a bunch of includes (bootstrap and stuff). This is liberally included across component scss files with the expectation it would be de-duplicated in the build.

However I realized my dev build was up to 150MB for the main.bundle.js and it was hammering my workstation when it built. By re-structuring the scss to only include was was necessary in each component I bought this down to like 11MB.

For the aot build it was less significant (I think most of the size was from sourcemaps) but still went from:

Date: 2018-02-15T16:02:33.684Z
Hash: 41fbbec562aa54786309
Time: 208050ms
chunk {scripts} scripts.d12170c009efb4ef2dbb.bundle.js (scripts) 209 kB [initial] [rendered]
chunk {0} main.0cafaa559bb3700ad50f.bundle.js (main) 18.4 MB [initial] [rendered]
chunk {1} polyfills.700d53c47d7964ae8cad.bundle.js (polyfills) 169 kB [initial] [rendered]
chunk {2} styles.c77032e78777d8b73cfb.bundle.css (styles) 282 kB [initial] [rendered]
chunk {3} inline.27eae89520a68a8b4864.bundle.js (inline) 1.45 kB [entry] [rendered]

to

Date: 2018-02-15T15:56:45.830Z
Hash: 147dec00b5e7c81037ed
Time: 155488ms
chunk {scripts} scripts.d12170c009efb4ef2dbb.bundle.js (scripts) 209 kB [initial] [rendered]
chunk {0} main.9b030cbd40e05afd9bf7.bundle.js (main) 3.17 MB [initial] [rendered]
chunk {1} polyfills.700d53c47d7964ae8cad.bundle.js (polyfills) 169 kB [initial] [rendered]
chunk {2} styles.c77032e78777d8b73cfb.bundle.css (styles) 282 kB [initial] [rendered]
chunk {3} inline.5c3035ae831e1473f3d7.bundle.js (inline) 1.45 kB [entry] [rendered]

I tried to track the memory usage as well. It seemed like with the improved scss it maxd out at about 1300MB memory to do the aot build. With the old scss setup it was similar but briefly spiked up to over 2.5GB at the end of the process. So it could be that memory useage is also affected by the duplicated scss.

(I'm using cli v1.7.0-beta.0 btw. rc.0 breaks my fonts in aot).

edit: slightly more scientific readout of memory usage:

Inefficent scss includes: old-scss

More efficent scss: new-scss

Not much in it really.

also improved scss with cli v1.7.0-rc.0 instead of beta.0:

plot1

and v1.6.8:

plot1

warmans avatar Feb 15 '18 16:02 warmans

I've installed increase-memory-limit and after I run it, although it ran successfully and it changed the files in the local project folder (I checked the files), the ram usage was still somewhere 1400 mega bytes and not more.

What I did after that is. I went to the folder where npm is installed which in my case was

C:\Users\USER\AppData\Roaming\npm

and I opened ng.cmd and edited it and added --max-old-space-size=10240 in both places (in the if and else clause)

2

Only after I did that and tried ng build --prod it started to use more ram.

Here in the photo you can see that I'm running two command prompts. The first one is after I ran increase-memory-limit, and the second one is after I edited the ng.cmd file manually. You can see that it started to use 3000 mega bytes.

1

Before doing this edit, the rare times that it worked (to build the project) it took me 660s and now it did it for 295s.

I hope it will help you.

ZgjimDida avatar Feb 15 '18 20:02 ZgjimDida

@ZgjimDida its working for me for @angular/[email protected] after added --max-old-space-size=10240 in both places

kanji-keraliya avatar Feb 16 '18 11:02 kanji-keraliya

Updating typescript version in package.json to the latest 2.4.x to 2.7.x, fixed the issue for me

FOSSKolkata avatar Feb 16 '18 12:02 FOSSKolkata

Adding SWAP will help in solving this error?

vasanthm-bx avatar Feb 16 '18 12:02 vasanthm-bx

@kanji-keraliya I'm glad I could help.

ZgjimDida avatar Feb 16 '18 15:02 ZgjimDida

@FOSSKolkata But I'm getting the warning of > @angular/[email protected] requires a peer of typescript@>=2.4.2 <2.7 but none is installed. You must install peer dependencies yourself.

jobnte avatar Feb 21 '18 16:02 jobnte

my project compiles on local machines but not in the CI due out of memory errors (8gb ram , 2gb swap). setting max-old-space-size is not a viable option for our CI pipelines.

solved updating typescript to 2.7.x but a nasty warning about 'ng compiler not supporting that version of typescript' shows up.

pavildon avatar Mar 06 '18 18:03 pavildon

this is an issue for us too; we are using a hosted VSTS build environment;

node: v 8.x npm: 5.6.0 @angular/cli: 1.7.2

I tried running npm install -g increase-memory-limit && increase-memory-limit prior to running the build command (node ./node_modules/@angular/cli/bin/ng build --prod --e=beta --sourcemaps=true), but it did not help. I still get OOM error as indicated above.

EDIT: updating the build command to the following, seems to have 'fixed' the issue. node --max_old_space_size=10240 ./node_modules/@angular/cli/bin/ng build --prod --e=beta --sourcemaps=true

joshvito avatar Mar 07 '18 21:03 joshvito

Facing the same issue.. Tried increasing the memory allocation which resolved this for a while, but after couple of days, things have started to fail again continuously.

aot

Increasing the memory limit might just be a patch and never resolve what is causing this. Any idea?

roymj88 avatar Mar 12 '18 10:03 roymj88

it seems that they dont care about this.

pavildon avatar Mar 12 '18 19:03 pavildon

@roymj88 Largely the performance issues have stemmed from build optimizations introduced in webpack that were incredibly expensive in terms of RAM usage for build. At least two of these issues were found and resolved in webpack itself, in part through the help of members of the angular-cli team.

So think it's really important to state what version you're using, as it may have been fixed for you, and roughly how large your project is. As depending on your size you will hit that wall eventually. Where the total build time and I think RAM usage does scale at least linearly with number of files.

So a quick-fix is indeed to simply increase the RAM usage even further. Alternatives to that:

  1. Isolate the issue into a public repository or simply share your code if that is possible. Will help them a lot in trying to triage the issue.
  2. Turn off build-optimizer (it's on by default for production). If you're not concerned about payload size of your app turning this flag off will likely speed up your production build significantly.
  3. Upgrade to Angular 6 beta and angular-cli 6 beta. The latest version uses Webpack 4 along with some other optimizations that supposedly should make builds faster.

it seems that they dont care about this.

@aindigo I think that's incorrect. There are steady performance improvements both in the cli and more importantly in Webpack, which the angular-cli sometimes contribute themselves. Bazel as an alternative to Webpack will very likely become a very much more performant alternative while version 6 does seem to come with a fair share of improvements to the build speed in general.

You could argue for different prioritization, with the big drawback of delaying other improvements and features like Service Workers and schematics.

Regardless, think the team is doing an awesome job and hope to continue see improvements in version 6 and onwards.

Koslun avatar Mar 13 '18 02:03 Koslun

@Koslun The versions are as follows:

  • angular-cli: 1.2.4
  • angular: 4.1.3
  • npm: 3.10.8
  • node: v6.9.1

The memory limit has been stretched to a whopping 2GB(2097152) and the builds are still failing with the heap memory issue after 92%.

BTW, why is this closed?

Regards Roy

roymj88 avatar Mar 16 '18 05:03 roymj88