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

@radoslavpetranov I have been thinking about that (@randyaa said the same thing as you), and trying to figure out what a good approach is.

Something that springs to mind is to split up into multiple processes. But upon further scrutiny that approach doesn't really help in the memory usage problem, since multiple process will use at least the same total memory, probably more due to inefficiencies and duplication.

The 1.5 problem highlighted how much Build Optimizer actually helps keep memory usage down by trimming imports (and thus project size). So maybe that's an overall better approach. It does imply that a project must be able to use Build Optimizer though, and there might still be problems with it (code manipulation is tricky).

Another approach might be to took a look at the Uglify memory usage in large bundles. Maybe there room for config tuning or something. But it's hard to say since those tools really rely on a semantic understanding of code bundles to work, so one does expect a lot of memory used on larger bundles.

Something else that I've been thinking about is to actually have a low memory setting, where disk/cpu/total time is traded for a smaller memory footprint. This is just a very high level idea that I haven't explored at all. It would help on limited environments like CI, whereas for dev environments you really want the tradeoffs that give you better rebuild/speeds instead.

For really large projects you just can't get around the fact there are more performant languages than JS and that build tools that can use them effectively (like Bazel) will offer a better experience.

filipesilva avatar Nov 15 '17 12:11 filipesilva

@filipesilva - Thanks for investigating this. Here's another data point, fwiw:

What version of the CLI and Angular are you on? 1.5.0 and 5.0.0 Does this only happen on ng build --prod? Seems to be building fine without the --prod flag

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

If you have a project that I can look at, can you link it please? Sorry, private repository and project

erlloyd avatar Nov 15 '17 14:11 erlloyd

@filipesilva Thanks for the update and explanation, however we experience this with our projects using cli 1.4.7 and angular 4.4.5 (and early versions too). Currently to build our production version we use the following:

ng build --environment=prod --aot --sourcemaps=false --base-href=\"/ui/\" --preserve-symlinks --output-hashing=all --extract-css=true

We then pass the output to Uglify to minify and mangle the JS and CSS. This process works correctly and we do not hit memory issues, but when we build with ng build --prod that is when we run into memory issues.

We also do not have the devextreme lib. I can't share our code, but I'm happy to install other versions of the CLI and post build results from different commands. Our application is approx 450 components and 290 modules.

mattem avatar Nov 15 '17 16:11 mattem

Hello @filipesilva
I don't know exactly if this my comment might be useful, but I will report some tests that I did.

I have an angular-cli (1.4.1), angular (4.4.4), typescript (2.3.4) project. This project contains approximately 200 services and more than 700 components, distributed in at least 15 modules.

When running ng build --prod I noticed a few things. In this specific project, 0 to 1.9 GB of memory occurs up to the first 10% of the compilation. (I'm using the saas files from bootstrap).

image

From the 11% up to the 89% compilation, my memory goes up to less than 3 GB.

From 90% to 92% the memory is used in 4.6 GB.

And finally at 95% the consumption goes up to the maximum limit allowed. In my case now, up to the 6 GB of RAM I've bound.

image

To make it compile, I had to configure virtual memory (swap). I have only 8 GB RAM.

I increased to 12 GB, 16 GB, until finally I was able to compile the project by setting 32 GB RAM.

Consumption of NodeJS hit 28 GB at these same 95% emitting.

Unfortunately this project is private and would not be able to make it available.

Tests requested:

ng build --build-optimizer=false works! 1.5 GB max.

ng build --aot error!

To try to help, I created a very simple project. In it, there are 10 modules created. In each module, there are 50 components created. In each component there is only one HTML with output "works!".

There is no CSS, there is no third-party npm component. A simple project like this is consuming 1.6 GB of RAM for the AOT build.

image

I believe that consumption is a little high because it contains absolutely nothing in the project. If you wanted to download the repository to test, it is zipped in the link below. http://www.codegenerator.com.br/downloads/ng-heap-memory.zip

Thanks in advance for your help.

eduardotolino avatar Nov 15 '17 19:11 eduardotolino

I've put up a PR that should specifically address the memory usage regression when using 1.5 (https://github.com/angular/angular-cli/pull/8506). Once it's merged and released I'll ping you all here to see if it helped across your projects.

@Tolitech your report of how memory usage changed with build stage was very interesting and useful. Below the 92% stage what happens is TypeScript compilation and loading files into Webpack. After that it's mostly Uglify and Build-Optimizer.

But since you are on 1.4.1 you shouldn't have Build Optimizer by default with --prod. Just to double check, can you confirm that you are not using --build-optimizer?

In your report you mention using what I consider a large project (200 services and 700 components). Definitely not the largest around but still big. I'm not surprised at a 3GB memory usage for TS compilation and loading all app files.

But when it starts getting really bad is after that, when you end up needing at least 28GB at the 92% phase. This to me tells me the bottleneck is Uglify, or maybe some other part of our build pipeline that I'm not seeing right now. But it definitely bears investigating, which I'll do.

Running the small ng-heap-memory project as is I saw a maximum of 1.4GB ram being used. Using the PR this dropped to around 800MB, so it seems to be helping there. I agree that the 500 components are essentially empty, but it doesn't surprise me that just the sheer number of them causes high memory consumption on a build. It's a valuable example by the way, and thank you for making it.

@mattem that's a very interesting approach, and doubly interesting is that you don't suffer much memory issues with it. Can I ask you to give me some concrete numbers for comparison please? Maximum memory usage on a CLI prod build versus CLI non-prod plus manual Uglify. I'd also like to know your uglify version and settings please.

filipesilva avatar Nov 15 '17 21:11 filipesilva

Hello, @filipesilva Yes, I confirm that I'm using without --build-optimizer. Just ng build --prod.

And thanks for the feedback.

eduardotolino avatar Nov 15 '17 22:11 eduardotolino

@filipesilva i used same workaround i described it in my comment https://github.com/angular/angular-cli/issues/5618#issuecomment-343224021

command ng build --app homeapp -aot=true -e=prod -sm=false -ec=true --named-chunks=false -oh=all --build-optimizer=true -vc=true

my memory usage: 0 % -18 % <= 828 mb 18% -89 <= 1140 mb 90 - 100 < 1253 mb

difference between --build-optimizer=true and false 90 mb

for uglify used gulp-uglify^3.0.0 default settings 1058 mb also i used gulp-postcss for uglify and autoprefixer all latest version

@angular 5.0.1 @angular/cli 1.5.0

WandererInVoids avatar Nov 15 '17 22:11 WandererInVoids

@filipesilva Using ng build --prod gave the OOM error, however running the CLI with node --max_old_space_size=8192 it did complete, with the following memory usage (observed from looking at Activity Monitor in MacOS):

  • ~ 14% 800mb real / 5.5gb virtual
  • ~ 69% 1.5gb real / 6.4gb virtual
  • ~ 90% 1.8gb real / 6.65gb virtual
  • ~ 92% 2.3gb real / 7gb virtual
  • ~ 94% 2.6gb real / 7.12gb virtual

Running with ng build --environment=prod --aot --sourcemaps=false --base-href="/ui/" --preserve-symlinks --output-hashing=all --extract-css=true (no increasing max_old_space) tops out at 1.49gb real / 6.23gb virtual. This is running with @angular/[email protected] and @angular/*@4.4.5

We use [email protected], giving us [email protected], setting compress and mangle to true. The output is also run though loose-envify, uglifycss, and htmlmin The gulp task peaks at 868mb real / 5.57gb virtual.

mattem avatar Nov 16 '17 01:11 mattem

According to changelog the above mentioned fix is included in 1.5.1+. It still fails for ng build --prod with 1.5.2 :(

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

[4980:000001B75DBAB8D0] 268298 ms: Mark-sweep 1399.5 (1698.5) -> 1399.5 (1698.5) MB, 483.4 / 0.0 ms allocation failure GC in old space requested [4980:000001B75DBAB8D0] 268868 ms: Mark-sweep 1399.5 (1698.5) -> 1399.5 (1629.5) MB, 569.7 / 0.0 ms last resort GC in old space requested [4980:000001B75DBAB8D0] 269387 ms: Mark-sweep 1399.5 (1629.5) -> 1399.5 (1600.0) MB, 518.8 / 0.0 ms last resort GC in old space requested

<--- JS stacktrace --->

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

Security context: 00000185B7C25EC1 1: DoJoin(aka DoJoin) [native array.js:~95] [pc=0000002F55357679](this=00000075A3182311 ,p=0000001FB1A5B831 <JSArray[937]>,q=937,E=00000075A31823B1 ,A=00000185B7C6EC69 <String[1]: >,z=00000075A3182421 ) 2: Join(aka Join) [native array.js:~120] [pc=0000002F550C6489](this=00000075A3182311 ,p=0000001FB1A5B831 <JSArray[937]>,q=937,A=00000185B7C6EC69 <Str...

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

PeS82 avatar Nov 17 '17 03:11 PeS82

Still encountering this issue with angular cli 1.5.2

`ng build --aot --prod 95% emitting
<--- Last few GCs --->

209426 ms: Mark-sweep 1512.7 (1406.8) -> 1512.5 (1406.8) MB, 453.1 / 0.0 ms [allocation failure] [GC in old space requested]. 209905 ms: Mark-sweep 1512.5 (1406.8) -> 1513.6 (1403.8) MB, 479.5 / 0.0 ms [last resort gc]. 210359 ms: Mark-sweep 1513.6 (1403.8) -> 1514.7 (1403.8) MB, 453.6 / 0.0 ms [last resort gc].

<--- JS stacktrace --->

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

Security context: 0x4353ac3fa99 1: SparseJoinWithSeparatorJS(aka SparseJoinWithSeparatorJS) [native array.js:~75] [pc=0x1e05abc97c4a] (this=0x4353ac04241 ,w=0x1a58c504381 <JS Array[1078]>,F=0x1a58c5043a1 <JS Array[1078]>,x=1078,I=0x37d7075bc209 <JS Function ConvertToString (SharedFunctionInfo 0x4353ac5dbc9)>,J=0x4353ac4a369 <String[1]: >) 2: DoJoin(aka DoJoin) [native array.js:~129] [pc=0x1e05abb3c1b6] (thi...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: node::Abort() [/usr/local/bin/node] 2: node::FatalException(v8::Isolate*, v8::Localv8::Value, v8::Localv8::Message) [/usr/local/bin/node] 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node] 4: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/usr/local/bin/node] 5: v8::internal::Runtime_SparseJoinWithSeparator(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node] 6: 0x1e0596e060c7 Abort trap: 6 Jays-MacBook-Pro:ngx-dynamic-dashboard-framework jayhamilton$ ng --version

_                      _                 ____ _     ___

/ \ _ __ __ _ _ | | __ _ _ __ / | | | | / △ \ | ' \ / _ | | | | |/ _ | '| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/

Angular CLI: 1.5.2 Node: 6.11.1 OS: darwin x64 Angular: 5.0.1 ... animations, common, compiler, compiler-cli, core, forms ... http, platform-browser, platform-browser-dynamic, router

@angular/cdk: 2.0.0-beta.12 @angular/cli: 1.5.2 @angular/material: 2.0.0-beta.12 @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 @ngtools/webpack: 1.8.2 @schematics/angular: 0.1.5 typescript: 2.6.1 webpack: 3.8.1 `

jayhamilton avatar Nov 17 '17 03:11 jayhamilton

@jayhamilton +1 https://github.com/swimlane/ngx-datatable/issues/1104#issuecomment-342528502

finalxcode avatar Nov 17 '17 07:11 finalxcode

Hey all, 1.5.2 includes the fix for the 1.5 memory usage regression. This should help users that had projects that did not have memory problems on 1.4 but started having them in 1.5.

Projects that were already having memory problems in 1.4 should see no change. I'm still looking into the balooning memory requirements for the 92% bundle optimizing phase.

@PeS82 @jayhamilton @finalxcode you reported that you're still running into memory problems with 1.5.2. Was this on projects that did not have memory problems in 1.4.x?

filipesilva avatar Nov 17 '17 09:11 filipesilva

@filipesilva With angular cli 1.4.x it used to work, then when i upgrade angular/cli to 1.5.2, it have memory problems, i have to use command node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --aot to build

finalxcode avatar Nov 17 '17 09:11 finalxcode

@filipesilva with cli 1.5.2 my project fails at 95%. With angular cli 1.4.x it used to work.

JackArlington avatar Nov 17 '17 09:11 JackArlington

@filipesilva @JackArlington same for me. Our project used to build (prod) on 1.4.x and since 1.5.0 does not anymore. 1.5.2 didnot improve the situation. When I increase the memory limit it does compile, but uses aprox 5GB on emitting.

On a side not the huge memory consuption occurs on "95% emitting"

Shadowlauch avatar Nov 17 '17 09:11 Shadowlauch

@filipesilva @Shadowlauch I have just recognized that the machine I tested before had node 6.x upgrading it to 8.9.1 solved the problem and ng build --prod --aot works now. Thank you for the help.

JackArlington avatar Nov 17 '17 09:11 JackArlington

To be clear, I am not aware of any memory issue related specifically to Node 6. I have been testing with Node 8.9.1 myself because I couldn't get memory timelines using Chrome Devtools with Node 6.

Maybe that's part of the equation here... I expect newer nodes to be faster and more memory efficient but perhaps there's more to it.

For instance, I couldn't repro the problem on @seanlandsman's project. @seanlandsman what version of node were you using?

filipesilva avatar Nov 17 '17 10:11 filipesilva

I had node 6.x aswell and after upgrading to 8.9.1 it works now. @JackArlington thanks for the tip!

Shadowlauch avatar Nov 17 '17 10:11 Shadowlauch

@filipesilva I'm on node 7.7.3

Edit: I've just upgrade the test repo to 1.5.2 and retried - it still fails I'm afraid.

Did you try running with "node --stack_size=512 node_modules/.bin/ng build --prod" to see if this helps reproduce what I'm seeing?

thanks again

seanlandsman avatar Nov 17 '17 10:11 seanlandsman

@filipesilva I do not get heap out of memory error anymore with cli 1.5.2. My node version is 7.5.0. However the build still takes about 8 - 10 minutes to finish.

changLiuUNSW avatar Nov 17 '17 10:11 changLiuUNSW