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

@filipesilva due your comment i commented out that lines memory usage decreased from 5.5 gb to 2.5 gb

WandererInVoids avatar Nov 20 '17 14:11 WandererInVoids

@WandererInVoids ok, that's a good sign. I'll keep pressing on this line of investigation.

@Tolitech using your repo (CLI 1.4.1/ Angular 4.4.6 / TS 2.3.3) :

  • ng build peaks at 2.2GB ram
  • ng build --prod peaks at 18.5GB ram

The memory usage per progress % looks like this:

  • 0-11% - 3gb peak (ngtools/webpack)
  • 11%-89% - 5.2gb peak (loading and parsing ~4.3k modules into webpack)
  • 90%-94% - 8.0gb peak (module concatenation, uglify)
  • 95-100% - 18.5gb peak (emitting)

I also tried to upgrade the repro to use CLI 1.5.2/ Angular 5.0.2 / TS 2.4.2.

  • ng build peaks at 2.3GB ram
  • ng build --prod peaks at 22.0GB ram

The memory usage per progress % looks like this:

  • 0-11% - 3.3gb peak (ngtools/webpack, build-optimizer)
  • 11%-89% - 4.0gb peak (loading and parsing ~4.3k modules into webpack)
  • 90%-94% - 8.6gb peak (purify, module concatenation, uglify)
  • 95-100% - 22.0gb peak (emitting)

Note: using CLI 1.5 + Angular 5 defaults to using Build Optimizer on prod builds. I also noticed that the 92% phase took a lot longer than before like @changLiuUNSW reported, while memory usage kinda plateaued. It makes me think that PurifyPlugin (part of Build Optimizer) is what takes so long here.

For comparison, I also tried compiling the project using ngc and it peaked at 3.3gb used memory. Comparing that to the 0-11% phase it looks like our compiler plugin doesn't add a significant overhead. tsc itself peaks 830MB used memory.

The project using Angular 5 can be found at https://github.com/filipesilva/angular-cli-test-repo.

Here's a breakdown of the project files by extension:

1551 html
   1 ico
   8 jpg
   5 json
   5 png
  46 scss
2637 ts

Looking at the peak memory usage numbers I can say a couple of things:

  • Memory usage from compilation (0-11% phase) seems reasonable for project size.
  • The 11%-89% phase takes a while loading everything but it's also not very surprising.
  • I expected the 90%-94% phase to take the lion's share of memory usage but that wasn't the case at all.
  • The 95-100% phase is where memory is being massively used up.

I'm going to follow up with the Webpack folks to see what's actually happening in that last phase.

@Tolitech you've been a massive help getting to the bottom of this, I can't thank you enough :100:

filipesilva avatar Nov 20 '17 16:11 filipesilva

I don't get how 3.3 Gb memory for 1500 HTML files is reasonable. That's 2 Mb per file. Do these numbers contain collectable garbage or is this what's actually claimed until the end of the build?

Tragetaschen avatar Nov 20 '17 18:11 Tragetaschen

@filipesilva My setup is like this: CLI 1.5.2/ Angular 5.0.2 / TS 2.4.2 I use a few 3rd party packages.

ng build --prod : fails with the JavaScript heap out of memory error ng --env=build --aot : works

kdubious avatar Nov 20 '17 19:11 kdubious

I ran into this issue. I was able to get a successful build with node --max_old_space_size=8192 node_modules/.bin/ng build --prod

emilio-martinez avatar Nov 21 '17 02:11 emilio-martinez

To add on @emilio-martinez's answer, on Windows use: node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

AndreaBarbasso avatar Nov 21 '17 08:11 AndreaBarbasso

ng build --aot --prod
 95% emittingFATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory

Angular CLI: 1.5.2
Node: 8.9.1
OS: win32 x64
Angular: 5.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

@angular/cdk: 5.0.0-rc0
@angular/cli: 1.5.2
@angular/material: 5.0.0-rc0
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.36
@ngtools/json-schema: 1.1.0
@schematics/angular: 0.1.5
typescript: 2.4.2
webpack: 3.8.1

finalxcode avatar Nov 21 '17 09:11 finalxcode

@tolitech FYI I opened a webpack issue for the ModuleConcatenationPlugin memory usage: https://github.com/webpack/webpack/issues/5992.

@Tragetaschen it's not just the HTML files, it's all the TS and code transformations too. The amount of memory used by the plugin (which is developed in this repo) is reasonable insofar as it is not a big overhead, 3 vs 3.3gb, over the ngc compiler itself (which is developed in angular/angular).

filipesilva avatar Nov 21 '17 12:11 filipesilva

Thanks, @filipesilva

Taking advantage of the moment, I do not know if this would be the best time, but is there any technique for development to be improved?

Example: In a large project like this, a simple change in HTML or TS ends up taking a long time to reflect the onscreen settings. (ng serve -o running).

eduardotolino avatar Nov 21 '17 12:11 eduardotolino

@tolitech I'd like to talk about the different factors influencing dev rebuilds in big projects but would much prefer to do it in a separate issue. It's something that gets asked about frequently and it would be good to have discussion on it.

Can you open an issue called "Total rebuild time in medium and large projects", share your experience (rebuild time in the console but also time until app is ready in the browser), and ping me there please?

filipesilva avatar Nov 21 '17 13:11 filipesilva

@filipesilva I have tried commenting out the ModuleConcaternationPlugin and rerun our builds. In short our project still runs into memory issues.

I don't know if this makes a difference, but the way our projects are currently setup we have a number of internal libraries that while in development, use npm link to link.

Memory peaks at 2.51gb real / 7.26 virtual when running with the ModuleConcaternationPlugin commented out. Full output:

Matts-MacBook-Pro:mediator-ui matt$ node --max_old_space_size=8192 ./node_modules/.bin/ng build --prod --preserve-symlinks
 14% building modules 37/38 modules 1 active ...pace/html/mediator-ui/src/styles.scssTemplate parse warnings:
The <template> element is deprecated. Use <ng-template> instead ("      <span *ngIf="!itemTemplate">{{field ? option[field] : option}}</span>
                        [WARNING ->]<template *ngIf="itemTemplate" [pTemplateWrapper]="itemTemplate" [item]="option"></template>
        "): ng:///Users/matt/Documents/workspace/html/mediator-ui/node_modules/primeng/components/autocomplete/autocomplete.d.ts.AutoComplete.html@22:24                           Date: 2017-11-21T13:58:28.746Z                                                       Hash: 683f2d7486502f8e507a
Time: 252811ms
chunk {0} 0.3a309735936ea414db80.chunk.js (common) 581 kB {20}  [rendered]
chunk {1} 1.8caff8c57661c1307d98.chunk.js () 2.55 kB {20}  [rendered]
chunk {2} 2.b3d1517bf29cec331f56.chunk.js () 179 kB {20}  [rendered]
chunk {3} 3.dd257d4e2f9e4640b2d4.chunk.js () 164 kB {20}  [rendered]
chunk {4} 4.e2edee33bd5ed8a53124.chunk.js () 116 kB {20}  [rendered]
chunk {5} 5.13f271efca121303c5fc.chunk.js () 83.2 kB {20}  [rendered]
chunk {6} 6.08af5ef8c2fe3e09267a.chunk.js () 36.8 kB {20}  [rendered]
chunk {7} 7.8ec810fd30855fef71f7.chunk.js () 30 kB {20}  [rendered]
chunk {8} 8.15f033cf28671ef4988a.chunk.js () 29.5 kB {20}  [rendered]
chunk {9} 9.151ee32b9cc676f2034e.chunk.js () 31.1 kB {20}  [rendered]
chunk {10} 10.cb4d037fcc697b055929.chunk.js () 42.7 kB {20}  [rendered]
chunk {11} 11.f2584b57685d1109a976.chunk.js () 37.5 kB {20}  [rendered]
chunk {12} 12.fe05dccec88da4ae948c.chunk.js () 29.4 kB {20}  [rendered]
chunk {13} 13.2028975a01820ba8496f.chunk.js () 6.1 kB {20}  [rendered]
chunk {14} 14.3be6220b9707e6312847.chunk.js () 3.12 kB {20}  [rendered]
chunk {15} 15.5113d2a9539e99c7ad7f.chunk.js () 6.15 kB {20}  [rendered]
chunk {16} 16.915b0b2f0ed403ce914b.chunk.js () 5.66 kB {20}  [rendered]
chunk {17} 17.485ba9c96f0e3aa081f0.chunk.js () 3.06 kB {20}  [rendered]
chunk {18} 18.db6e52da7740aeca6bb0.chunk.js () 13 kB {20}  [rendered]
chunk {19} polyfills.efb98b65fb45848a1cc3.bundle.js (polyfills) 170 kB {23} [initial] [rendered]
chunk {20} main.4a056bb640e0da824df3.bundle.js (main) 49 kB {22} [initial] [rendered]
chunk {21} styles.e50be95f68f4bf889ab4.bundle.css (styles) 263 kB {23} [initial] [rendered]
chunk {22} vendor.f23a5bbcd6f969d5bec4.bundle.js (vendor) 5.1 MB [initial] [rendered]
chunk {23} inline.35b8b1727b921c4ee6ae.bundle.js (inline) 1.94 kB [entry] [rendered]

I then tried with the UglifyJsPlugin commented out: Memory peaks at 1.54gb real / 6.35 virtual.

Matts-MacBook-Pro:mediator-ui matt$ ng build --prod --preserve-symlinks
 14% building modules 37/38 modules 1 active ...pace/html/mediator-ui/src/styles.scssTemplate parse warnings:
The <template> element is deprecated. Use <ng-template> instead ("      <span *ngIf="!itemTemplate">{{field ? option[field] : option}}</span>
                        [WARNING ->]<template *ngIf="itemTemplate" [pTemplateWrapper]="itemTemplate" [item]="option"></template>
        "): ng:///Users/matt/Documents/workspace/html/mediator-ui/node_modules/primeng/components/autocomplete/Date: 2017-11-21T12:31:03.567Z                                                       Hash: a93f716c1a9cd5e34227
Time: 205392ms
chunk {0} 0.7f726b90dbfda77c6fd2.chunk.js (common) 1.78 MB {20}  [rendered]
chunk {1} 1.b2415c8c032817feee41.chunk.js () 9.49 kB {20}  [rendered]
chunk {2} 2.5dbd68c68fe419faaac6.chunk.js () 20.2 kB {20}  [rendered]
chunk {3} 3.f65ad52f00741668ecea.chunk.js () 325 kB {20}  [rendered]
chunk {4} 4.c60ce468892788b134f0.chunk.js () 11.2 kB {20}  [rendered]
chunk {5} 5.0e510c127917b57b977e.chunk.js () 112 kB {20}  [rendered]
chunk {6} 6.17a1081f0b32fa32114a.chunk.js () 116 kB {20}  [rendered]
chunk {7} 7.0db6a1bd5ef8cd7389e2.chunk.js () 145 kB {20}  [rendered]
chunk {8} 8.08dea29d9d56c08ac135.chunk.js () 133 kB {20}  [rendered]
chunk {9} 9.6c20abd2e257ccb42f3a.chunk.js () 112 kB {20}  [rendered]
chunk {10} 10.245dd4aebc6e15998e11.chunk.js () 455 kB {20}  [rendered]
chunk {11} 11.3a63d64df6659b591bc7.chunk.js () 91.8 kB {20}  [rendered]
chunk {12} 12.4bdb534a025025ab67c5.chunk.js () 28.8 kB {20}  [rendered]
chunk {13} 13.58f953974b1a1cab76cd.chunk.js () 27 kB {20}  [rendered]
chunk {14} 14.a295fb06cc46ee37d6dd.chunk.js () 18 kB {20}  [rendered]
chunk {15} 15.573a4c4817db899cbaa5.chunk.js () 522 kB {20}  [rendered]
chunk {16} 16.1bef7d4db5d387785c53.chunk.js () 209 kB {20}  [rendered]
chunk {17} 17.2892448d1217c3964316.chunk.js () 10.2 kB {20}  [rendered]
chunk {18} 18.8a52fcf63d5fb567be17.chunk.js () 80.8 kB {20}  [rendered]
chunk {19} polyfills.efb98b65fb45848a1cc3.bundle.js (polyfills) 497 kB {23} [initial] [rendered]
chunk {20} main.1b1138cc3dcdfe7392ca.bundle.js (main) 174 kB {22} [initial] [rendered]
chunk {21} styles.e50be95f68f4bf889ab4.bundle.css (styles) 263 kB {23} [initial] [rendered]
chunk {22} vendor.2bca18ae1189b0ecdba1.bundle.js (vendor) 14.4 MB [initial] [rendered]
chunk {23} inline.8e041fa7d36f79764f2f.bundle.js (inline) 6.41 kB [entry] [rendered]

The build completed without the need to add additional memory to Node.

I also tried running with node --inspect and attaching the Chrome debugger to grab some heap snapshots and memory profiles, but it in my tests that caused the CLI to hang during the compilation stage with 1 module remaining.

The other part that is hammering us is the time taken by the compile for tests, we have had to continually increase timeouts in the Karma conf, and the time taken for a page refresh with tests (during development) is quite high, mainly due to that ~15mb vendor bundle.

mattem avatar Nov 21 '17 14:11 mattem

@filipesilva I get that there is "stuff" that's generated, that's why I asked if these are clean numbers or if remaining garbage from the actual processing is still contained in that number. If that's the actual number of bytes claimed, then I still don't get it: Assuming 2Kb per HTML file on average(!) (which is still much more than I would expect - I have 1 kb on average) then each single byte from those files (spaces, angular brackets, text characters, etc.) will generate 1000 bytes of transformed / generated content. I just cannot believe that's the case.

Tragetaschen avatar Nov 21 '17 15:11 Tragetaschen

@Tragetaschen the numbers I got were just looking at the task manager and keeping track of what the maximum used memory was. I'm not sure why you are focusing on the HTML templates though. ngc does more than just compile templates.

filipesilva avatar Nov 21 '17 18:11 filipesilva

@mattem if you're already reaching 1.54GB without Uglify/ModuleConcaternationPlugin, then it looks like your project as is needs more than the default node max memory. At how much memory does your project peak when doing just ng build?

filipesilva avatar Nov 21 '17 18:11 filipesilva

@mattem can you open an issue called "Total rebuild test time in medium and large projects", similar to the one I asked @Tolitech to open in https://github.com/angular/angular-cli/issues/5618#issuecomment-346024634? It's a broader topic as well.

filipesilva avatar Nov 21 '17 18:11 filipesilva

@Tragetaschen I measured tsc peak as well and it's 830MB. Bear in mind however that ngc both compiles templates, transforms code, and generate new TS files. So total TS files processed would be around double. I pinged the team about the difference between ngc and tsc.

filipesilva avatar Nov 21 '17 18:11 filipesilva

@filipesilva Running ng build uses 1.31gb real / 5.99 virtual peak.

Here is the full output from the command

Matts-MBP:mediator-ui matt$ ng build
Date: 2017-11-21T18:59:28.538Z                                                            
Hash: 5c68c24623b719d5f1b0
Time: 100214ms
chunk {brand-registration-task.module} brand-registration-task.module.chunk.js, brand-registration-task.module.chunk.js.map () 17.4 kB {main}  [rendered]
chunk {common} common.chunk.js, common.chunk.js.map (common) 562 kB {main}  [rendered]
chunk {custom-report-task-module} custom-report-task-module.chunk.js, custom-report-task-module.chunk.js.map () 7.4 kB {main}  [rendered]
chunk {episode-registration-task.module} episode-registration-task.module.chunk.js, episode-registration-task.module.chunk.js.map () 17.5 kB {main}  [rendered]
chunk {feature-not-available-view.module} feature-not-available-view.module.chunk.js, feature-not-available-view.module.chunk.js.map () 4.32 kB {main}  [rendered]
chunk {home-view.module} home-view.module.chunk.js, home-view.module.chunk.js.map () 5.46 kB {main}  [rendered]
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered]
chunk {job-management-dashboard-task-module} job-management-dashboard-task-module.chunk.js, job-management-dashboard-task-module.chunk.js.map () 8.61 kB {main}  [rendered]
chunk {main} main.bundle.js, main.bundle.js.map (main) 17 MB {vendor} [initial] [rendered]
chunk {material-registration-task.module} material-registration-task.module.chunk.js, material-registration-task.module.chunk.js.map () 18.4 kB {main}  [rendered]
chunk {package-editor-task-module} package-editor-task-module.chunk.js, package-editor-task-module.chunk.js.map () 18.3 kB {main}  [rendered]
chunk {placing-registration-task.module} placing-registration-task.module.chunk.js, placing-registration-task.module.chunk.js.map () 18.5 kB {main}  [rendered]
chunk {playlist-task.module} playlist-task.module.chunk.js, playlist-task.module.chunk.js.map () 23.4 kB {main}  [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 534 kB {inline} [initial] [rendered]
chunk {series-registration-task.module} series-registration-task.module.chunk.js, series-registration-task.module.chunk.js.map () 17.5 kB {main}  [rendered]
chunk {session.module} session.module.chunk.js, session.module.chunk.js.map () 18.4 kB {main}  [rendered]
chunk {spot-check-task-module} spot-check-task-module.chunk.js, spot-check-task-module.chunk.js.map () 6.3 kB {main}  [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 318 kB {inline} [initial] [rendered]
chunk {tasks.module} tasks.module.chunk.js, tasks.module.chunk.js.map () 26.2 kB {main}  [rendered]
chunk {timeline-task-module} timeline-task-module.chunk.js, timeline-task-module.chunk.js.map () 52.3 kB {main}  [rendered]
chunk {user-settings-view.module} user-settings-view.module.chunk.js, user-settings-view.module.chunk.js.map () 31.8 kB {main}  [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 5.07 MB [initial] [rendered]
chunk {views.module} views.module.chunk.js, views.module.chunk.js.map () 91.1 kB {main}  [rendered]
chunk {workflow-dashboard-task-module} workflow-dashboard-task-module.chunk.js, workflow-dashboard-task-module.chunk.js.map () 1.76 MB {main}  [rendered]

We don't run into memory issues (yet, I guess) when running development builds, we are generally experiencing the same as what @Tolitech has already mentioned. (I'm keeping an keen eye on the progress of the Bazel building and the work being done there, happy to be a guinea pig for any of that)

mattem avatar Nov 21 '17 19:11 mattem

@filipesilva Just to be sure: It's not that I care about the HTML compilation step per se, it's that you claimed 3.3 GiB memory consumption just for HTML and NgModule processing "reasonable for project size". Using a thousand times the memory compared to the input data size while the resulting bundle (even without any further optimizations) is over an order of magnitude smaller just isn't reasonable.

Sure, there are bigger problems when it comes to memory consumption right now and I wish I could me more (well, any kind of) constructive, but the entire compilation process currently takes one or two orders of magnitude (!) more memory than I would call "reasonable".

Tragetaschen avatar Nov 21 '17 19:11 Tragetaschen

@tragetaschen I think Felipe is commenting on your math. That step doesn't just involve the HTML file but includes the typescript and CSS files too. In order to properly uglify your entire project is brought into memory, so you can't just count the HTML. Additionally, some of your logic may cause duplication of subsets of your Dom, exploding the size. This is a complicated task that will take a good amount of RAM. We should use as little as possible, but we can't expect miracles.

markmaynard avatar Nov 21 '17 19:11 markmaynard

@filipesilva Opened issue #8579 for test rebuild times

mattem avatar Nov 21 '17 19:11 mattem