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 can confirm this issue, it looks a serious one.

It happens only when AOT is enabled.

So it will work fine if I disable AOT and run: ng build --prod --aot false

Angular CLI: 1.5.5 Node: 8.9.1 OS: linux x64 Angular: 5.0.5 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

@angular/cli: 1.5.5 @angular-devkit/build-optimizer: 0.0.34 @angular-devkit/core: 0.0.22 @angular-devkit/schematics: 0.0.39 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.5 @schematics/angular: 0.1.9 typescript: 2.4.2 webpack: 3.8.1

FrancescoBorzi avatar Dec 07 '17 18:12 FrancescoBorzi

1.6.0 seems to have fixed it for us. Our app compiled without extending the memory.

elclanrs avatar Dec 07 '17 19:12 elclanrs

@elclanrs not here, (1.6.0)

calebeaires avatar Dec 07 '17 20:12 calebeaires

apparently, upgrading to latest cli(1.6)/angular(5.1)/typescript(2.5) solved the issue

FrancescoBorzi avatar Dec 07 '17 20:12 FrancescoBorzi

I upgraded to cli 1.6 and angular 5.1 and now our app could build under 1.7 gigs

MrBlaise avatar Dec 08 '17 10:12 MrBlaise

@filipesilva a bit late, but I finally got around to testing ag-Grid with 1.6.0 and it all builds fine now.

Thanks for your hard work on this

seanlandsman avatar Dec 08 '17 14:12 seanlandsman

@filipesilva I tried the 1.6.0, it seems my project work fine, thank you.

alvachien avatar Dec 08 '17 17:12 alvachien

@filipesilva don`t work for me but build time and memory usage decreased

WandererInVoids avatar Dec 08 '17 18:12 WandererInVoids

Now it’s time to switch to Bazel/Rollup

aminebizid avatar Dec 08 '17 19:12 aminebizid

I upgraded to cli 1.6 and angular 5.1. The max memory the production build now using is around 1.3 GB. So no heap errors anymore.

Strange thing is that the process is 'hanging' at '92% chunk asset optimization' for 7 minutes of the total of 9 minutes buildtime. What is happening there?

JaapMosselman avatar Dec 11 '17 10:12 JaapMosselman

@Ristaaf thank you very much, your solution works perfect

handark avatar Dec 13 '17 23:12 handark

We still have this issue. We had this issue since angular@2 when I first time used angular CLI. Then I switched to angular starter and everything worked. Now Im trying to use angular cli again on the same project (upgrade from angular 4) and now I still have same issue with angular 5.1 and cli 1.6.2.

We have a really huge project and we are using angular on this project from early versions of angular since 2013.

When I simply use ng build build is going 1 hour. Here is a log.

When I use ng build I always have an error (after few minutes of build):

ng build
 10% building modules 3/3 modules 0 active                                         
<--- Last few GCs --->

[30628:0x102804600]   151498 ms: Mark-sweep 1405.8 (1664.5) -> 1405.8 (1664.5) MB, 1634.9 / 0.0 ms  allocation failure GC in old space requested
[30628:0x102804600]   152767 ms: Mark-sweep 1405.8 (1664.5) -> 1405.7 (1616.5) MB, 1268.5 / 0.0 ms  last resort 
[30628:0x102804600]   154066 ms: Mark-sweep 1405.7 (1616.5) -> 1405.7 (1601.0) MB, 1298.4 / 0.0 ms  last resort 


<--- JS stacktrace --->

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

Security context: 0x14c9d8b1cef1 <JSObject>
    1: set [native collection.js:~247] [pc=0x2fa4affd9607](this=0x166929284d21 <Map map = 0x20d66a1183b9>,p=0xb60e511a3c9 <NodeObject map = 0x78bae661b01>,x=0x3ce8d86d6ce9 <Object map = 0x3647e82a689>)
    3: record [/Users/pleerock/www/yakdu/ng5.yakdu.dev/node_modules/@angular/compiler-cli/src/transformers/node_emitter.js:135] [bytecode=0x214b10b7f429 offset=37](this=0x166929284ce9 <_NodeEmitte...

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::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::Factory::NewFixedArray(int, v8::internal::PretenureFlag) [/usr/local/bin/node]
 5: v8::internal::OrderedHashTable<v8::internal::OrderedHashMap, 2>::Rehash(v8::internal::Handle<v8::internal::OrderedHashMap>, int) [/usr/local/bin/node]
 6: v8::internal::Runtime_MapGrow(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
 7: 0x2fa4afe040dd
Abort trap: 6

If you want to know how many code and files we have:

cloc ./src/
    2705 text files.
    2680 unique files.                                          
https://github.com/AlDanial/cloc v 1.66  T=12.20 s (219.4 files/s, 17125.4 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
TypeScript                    2662          21207          15921         143472
CSS                              4           4084            203          18682
SASS                             6            772            305           4144
JSON                             3              0              0             68
HTML                             1              8              5             25
-------------------------------------------------------------------------------
SUM:                          2676          26071          16434         166391
-------------------------------------------------------------------------------

pleerock avatar Jan 03 '18 09:01 pleerock

@pleerock have you tried using the command to increase max memory, on both prod and dev builds?

If your project is near to the maximum memory it will need a lot of GC and that slows down things. Increasing the max memory even when you're not hitting it (like in dev) should help.

filipesilva avatar Jan 03 '18 10:01 filipesilva

@filipesilva thanks, it works. Both prod and dev builds. And build time is still 1 hour. Im glad that it works this way, however having to wait 1 hour each time is a problem. Also, its a big problem to run ng serve. When we run it, it takes same 1 hour to start serving which makes development process completely impossible.

pleerock avatar Jan 03 '18 13:01 pleerock

@pleerock yeah 1h for ng serve is really odd. I have never seen such a large build time in any projects I've handled.

I have reports of slow prod builds of that length (https://github.com/angular/angular-cli/issues/6795) but haven't heard of anything like that just for ng serve... I use https://github.com/filipesilva/angular-cli-test-repo as a benchmark for larger projects and it has around 2400 TS files, but still only takes a few minutes.

Is your repro public or is there a way that I can see it?

filipesilva avatar Jan 03 '18 13:01 filipesilva

Another option would be to take a CPU profile (there's some instructions here https://github.com/angular/angular-cli/issues/8259). But I wouldn't be surprised if the devtools crash, they usually do on huge jobs. Maybe a CPU profile of just a few minutes would be feasible?

filipesilva avatar Jan 03 '18 13:01 filipesilva

Is your repro public or is there a way that I can see it?

no, its not public. If you can we can make a call and I can show you and maybe we can use teamviewer or similar things.

BTW, 76% basic chunk optimization step takes most of time (99%). If I run ng serve and change a file it takes same 1 hour to rebuild everything.

CPU profile of first 10 minutes of 76% basic chunk optimization process (ng serve):

screenshot 2018-01-03 20 03 48

CPU-20180103T195000.cpuprofile.zip

pleerock avatar Jan 03 '18 15:01 pleerock

Thanks for getting me that CPU profile. So most of the time is spent on some array operations. It seems that this is happening as part of webpack module detection. All of the operations that take a long time happen on webpack/lib/optimizer/RemoveParentModulesPlugin.js.

image

@TheLarkInn does this ring a bell to you? I wonder if it's some kind of circular import problem.

@pleerock what version of webpack comes up when you do npm ls webpack btw?

filipesilva avatar Jan 03 '18 15:01 filipesilva

npm ls webpack

└─┬ @angular/[email protected]
  └── [email protected] 

pleerock avatar Jan 03 '18 15:01 pleerock

Try —stacksize 2048

Cordialement, Amine BIZID

Le 3 janv. 2018 à 10:58, Umed Khudoiberdiev [email protected] a écrit :

We still have this issue. We had this issue since angular@2 when I first time used angular CLI. Then I switched to angular starter and everything worked. Now Im trying to use angular cli again on the same project (upgrade from angular 4) and now I still have same issue with angular 5.1 and cli 1.6.2.

We have a really huge project and we are using angular on this project from early versions of angular since 2013.

When I simply use ng build build is going 1 hour. Here is a log.

When I use ng build -prod or ng build -prod -aot I always have an error (after few minutes of build):

ng build -prod -aot 10% building modules 3/3 modules 0 active
<--- Last few GCs --->

[30628:0x102804600] 151498 ms: Mark-sweep 1405.8 (1664.5) -> 1405.8 (1664.5) MB, 1634.9 / 0.0 ms allocation failure GC in old space requested [30628:0x102804600] 152767 ms: Mark-sweep 1405.8 (1664.5) -> 1405.7 (1616.5) MB, 1268.5 / 0.0 ms last resort [30628:0x102804600] 154066 ms: Mark-sweep 1405.7 (1616.5) -> 1405.7 (1601.0) MB, 1298.4 / 0.0 ms last resort

<--- JS stacktrace --->

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

Security context: 0x14c9d8b1cef1 1: set [native collection.js:~247] [pc=0x2fa4affd9607](this=0x166929284d21 ,p=0xb60e511a3c9 ,x=0x3ce8d86d6ce9 ) 3: record [/Users/pleerock/www/yakdu/ng5.yakdu.dev/node_modules/@angular/compiler-cli/src/transformers/node_emitter.js:135] [bytecode=0x214b10b7f429 offset=37](this=0x166929284ce9 <_NodeEmitte...

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::NewFixedArray(int, v8::internal::PretenureFlag) [/usr/local/bin/node] 5: v8::internal::OrderedHashTable<v8::internal::OrderedHashMap, 2>::Rehash(v8::internal::Handlev8::internal::OrderedHashMap, int) [/usr/local/bin/node] 6: v8::internal::Runtime_MapGrow(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node] 7: 0x2fa4afe040dd Abort trap: 6 If you want to know how many code and files we have:

cloc ./src/ 2705 text files. 2680 unique files.
https://github.com/AlDanial/cloc v 1.66 T=12.20 s (219.4 files/s, 17125.4 lines/s)

Language files blank comment code

TypeScript 2662 21207 15921 143472 CSS 4 4084 203 18682 SASS 6 772 305 4144 JSON 3 0 0 68 HTML 1 8 5 25

SUM: 2676 26071 16434 166391

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

aminebizid avatar Jan 03 '18 18:01 aminebizid