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

Happens for me also. Wired part is error thrown after bundle files generated.

Angular CLI: 1.5.0 Node: 8.9.0 OS: win32 x64 Angular: 5.0.0

<--- Last few GCs --->

[12192:00000000004C39A0] 236834 ms: Mark-sweep 1420.3 (2200.9) -> 1420.3 (2159.4) MB, 578.6 / 0.0 ms last resort GC in old space requested [12192:00000000004C39A0] 237461 ms: Mark-sweep 1420.3 (2159.4) -> 1420.3 (2136.9) MB, 627.2 / 0.0 ms last resort GC in old space requested

<--- JS stacktrace --->

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

Security context: 000000B4C7AA5EC1 1: DoJoin(aka DoJoin) [native array.js:~95] [pc=0000001391AFE299](this=000001A846C82311 ,p=00000050BD62B091 <JSArray[576]>,q=576,E=000001A846C823B1 ,A=000000B4C7AEEC69 <String[1 ]: >,z=000001A846C82421 ) 2: Join(aka Join) [native array.js:~120] [pc=0000001390C722A9](this=000001A846C82311 ,p=00000050BD62B091 <JSArray[576]>,q=576,A=000000B4C7AEEC69 <Str...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory npm ERR! code ELIFECYCLE npm ERR! errno 3 npm ERR! [email protected] build:prod: ng build --env=prod --prod --aot --stats-json --named-chunks=true --sourcemaps=false npm ERR! Exit status 3

==== update ====== Issue for me is --stats-json. Build is fine after remove this flag.

adamzhu-xs avatar Nov 08 '17 08:11 adamzhu-xs

Optimizations are welcome, but on the other hand memory is cheap. Until optimizations are here run the build using

node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build

and append whatever switches you'd like to the end. To make it easier to run, edit your package.json file and add this line to "scripts":

"serve": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng serve --aot"

Then you can execute it using

npm run serve

I'm an old Java dev, so this seems familiar to me somehow :)

swftvsn avatar Nov 08 '17 09:11 swftvsn

@swftvsn, memory is cheap if you have control over it, but we deploy to a Docker swarm, and containers have limited memory. This is a pretty serious issue for us. I'd prefer a slower build if that means caching stuff to disk and not running out of memory.

elclanrs avatar Nov 08 '17 14:11 elclanrs

I am getting this issue after upgrading to cli 1.5 and angular 5.0.

I want to highlight the question above:

  • Is Angular built for enterprise development?

I am so confused why any compiler would need 4 gigs of memory to compile a web app..... To get it to compile aot post upgrade I had to jack the memory up to 4 gigs. Our app has 2 feature module libraries each with about 15 modules, ~60 modules in the main app, and ~15 services. We have a lot left to build as well.

tim-white87 avatar Nov 08 '17 20:11 tim-white87

I would imagine it has to do with the obscene amount of dependencies + all the hoops that are done to statistically analyze all the dependencies and your code in order to shake the trees. Take a look at node_modules if you dare. (And source / debug maps and things I don't even know about)

Excessive memory consumption is usually caused by a lot of data and/or abstraction over abstraction over abstraction over... :) Or a bug.

For now I'm happy and I just feed the beast. I do hope it looses some of it's appetite in the coming versions though..

swftvsn avatar Nov 08 '17 22:11 swftvsn

I'm less concerned about "how could this possibly...?" than I am about the lack of communication.

The compilation itself is doing some pretty heavy lifting right now. It's really great actually. The entire CLI is. It bundles this thing up and serves it pretty compactly. In fact his may be a testament to just how easy Angular makes it to build large scale web apps... their size is outpacing the toolchain.

I'm just looking for the Angular team to set expectations here, that's all:

  • Does the Angular team consider this a problem? (its been tagged as "required" since March)
  • Is there any action being taken to fix it? (its been tagged as "investigation" since March)
  • Is this a fixable issue in the next month? 6 months? year?
  • If it's not a fixable issue, are there any other build configurations/toolchains they'd recommend?
  • Are they still researching the answers to the above questions?

Any answer to those questions is okay... but it's important that those answers (and their progress) are communicated to the community in an iterative fashion. My team and many other teams need to make decisions based off those answers, and right now we feel frozen.

This is an open source project that we're all getting for free. It's added tremendous value already even with this issue; so some people are being awfully critical given this. At the same time, open communication is integral to the success of solid open source projects: we're looking to the Angular team for that leadership.

@filipesilva , @hansl?

dpxxdp avatar Nov 09 '17 10:11 dpxxdp

That's an excellent summary @dpxxdp !

swftvsn avatar Nov 09 '17 11:11 swftvsn

Recent upgrade our project to Angular 5 and Angular CLI 1.5. We have 100+ components and 20+ modules. The build --prod is taking forever and finally throw heap out of memory exception which is very frustrating!

changLiuUNSW avatar Nov 09 '17 12:11 changLiuUNSW

As I mentioned here a few months ago, I have a very large project with several modules too, +120 pages, +500 components (pages are divided into at least 3 components like grid, form).

To get ng build -prod to work I had to test on a machine with 32 Gb of RAM, nodeJS consumed 28 Gb to compile the project. Impressive.

The solution was actually put 32 Gb as virtual memory on the machine (swap). In addition, of the already known "--max_old_space_size".

eduardotolino avatar Nov 09 '17 12:11 eduardotolino

The label of this issue should be priority: 1 (urgent) rather than priority: 2 (required)

changLiuUNSW avatar Nov 09 '17 12:11 changLiuUNSW

@swftvsn I am still not getting it. Maybe it is because I come from .NET where the c# compiler would tear through a project with comparable amounts of classes without breaking a sweat.

@Tolitech This is exactly the main point. If we need machines with 32 GB of ram to build the project, some cloud agents won't be able to handle this/aren't configurable to upgrade the hardware. Thus, we would need to make architectural decisions to start building segmented SPAs to keep the size of a project down, catering to compiler limitations.

@dpxxdp While I do agree that it is great that it is free and open source (thanks as well team!), I don't feel that it is a criticism to report a major bug/functional limitation that requires a hardware upgrade as a work around. I don't think anyone is trying to criticize the work that was done, but rather increase the priority on this issue.

tim-white87 avatar Nov 09 '17 16:11 tim-white87

guys 2 workaround for heap problem

  1. use custom build with webpack currently default build use several optimization step + minification.
  2. use command like ng build --app app -aot=true -e=prod -sm=false -ec=true --named-chunks=false -oh=all -pr=false this will generate bundles without minification and general size for vendor bundle will increased in my case for 8%. Build time decreased for twice, but minification step take long time 6-7 min in CI machine (2gb). I use gulp for post build actions in my case its uglify js and postcss.

My app ~72 services (CRUD operation + some general logic) ~10 pipe ~15 directive ~170 components ~ 80 model class (data from API) ~ 15 module

as for me i want some flags to set optimization step count, tree shake deep (function, class, variable)

WandererInVoids avatar Nov 09 '17 17:11 WandererInVoids

Hi all,

I've bumped the priority on this issue and will be working on it. We didn't consider it to be as urgent before because the solution at the time was to increase the available memory to node, which defaults to 1.7gb. So it was expected that at some point projects would need more memory than the default.

However with the release of 1.5 the memory requirements seem to have increased dramatically. Projects that didn't have any memory problems before now do. 32gb is definitely a ridiculous amount of RAM and shouldn't be needed.

At this point I'm not sure what caused the memory spike but have a few hypothesis. With CLI1.5 + Angular 5 we use a new pipeline that does better type checking by using a full TS program. Maybe there's a bad cache somewhere in there, or the TS program is taking a lot of memory. Another possibility is Uglify, which we had to upgrade as well. Or it might be something in the AOT compiler itself. We also default build optimizer to true now and it's not really optimized for neither speed nor memory.

If you're running into this problem and have time, I'd appreciate if you could answer a couple questions:

  • What version of the CLI and Angular are you on?
  • Does this only happen on ng build --prod?
  • Do you still get the memory error with these build commands:
    • ng build --prod --build-optimizer=false
    • ng build --aot
    • ng build --aot --build-optimizer
  • If you have a project that I can look at, can you link it please?

filipesilva avatar Nov 09 '17 18:11 filipesilva

In response to above comment: CLI 1.5.0, Angular 5.0.1

Project: Services: 9 Components: 43 Models: 28 Lines of ts: 8200 Lines of sass: 5000

Heap error happens with: ng build --prod: yes ng build --build-optimizer=false: no ng build --aot: no ng build --aot --build-optimizer: no

YeomansIII avatar Nov 09 '17 18:11 YeomansIII

@filipesilva About 20 modules, 30 services, 80 components

  • CLI 1.4.4, Angular 4.4.4
  • Only happens when building with --prod, --aot, and --sourcemap (we need it in prod...)
  • If settings memory to 8gb, we can actually use --prod, --aot, --sourcemap, AND --build-optimizer

Can't show the repo unfortunately

etiennetalbot avatar Nov 09 '17 19:11 etiennetalbot

@filipesilva

What version of the CLI and Angular are you on? 1.5.0 angular 5.0.1 Does this only happen on ng build --prod? yes Do you still get the memory error with these build commands: ng build --build-optimizer=false no ng build --aot no ng build --aot --build-optimizer no

WandererInVoids avatar Nov 09 '17 19:11 WandererInVoids

@filipesilva Windows 7 Pro 64-bit 12 Gigs of ram CLI 1.5.0, Angular 5.0.0

Project details: ~120 components 15 modules

Do you still get the memory error with these build commands: ng build --prod: yes ng build --build-optimizer=false: no ng build --aot: no ng build --aot --build-optimizer: yes

jmcclanahan avatar Nov 09 '17 19:11 jmcclanahan

On Angular 4.3 with ~180 components with ~30 services with ~22 modules

With CLI 1.5 Does this only happen on ng build --prod? Yes

ng build --build-optimizer=false less than 2GB ng build --aot --build-optimizer=false ~2.8GB ng build --aot over 3 GB

With CLI 1.4.5 Does this only happen on ng build --prod? Yes

ng build --build-optimizer=false less than 2GB ng build --aot --build-optimizer=false ~2.8GB ng build --aot ~2.8 GB

Our repo is private unfortunately

dpxxdp avatar Nov 09 '17 19:11 dpxxdp

CLI 1.5.0, Angular 5.0.0

Modules: ~50 Services: ~110 Components: ~290

ng build --prod: fail ng build --prod --build-optimizer=false: fail ng build --prod --build-optimizer=false --sourcemaps=false: fail ng build --prod --aot=false: works ng build: works ng build --aot=true: works ng build --aot=true --build-optimizer=true: fail

garkbit avatar Nov 09 '17 19:11 garkbit

What version of the CLI and Angular are you on? @angular/[email protected] , @angular 5.0

Does this only happen on ng build --prod? yes

Do you still get the memory error with these build commands: ng build --build-optimizer=false : NO ng build --aot : NO ng build --aot --build-optimizer: NO

AntonisFK avatar Nov 09 '17 21:11 AntonisFK