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

For us - building locally with ng build --target=production --environment=prod --base-href /myproject/ --aot --build-optimizer works just fine, but when we build it on our server, we get the FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory error. However, if we remove the --build-optimizer everything runs fine.

I've tried adding the increase-memory-limit library, but without any luck.

Is there a fix to this that actually works?

Kind regards Chris

chriseenberg avatar Aug 18 '17 12:08 chriseenberg

Tried latest version of typescript and still didn't help. Locally it builds but consumes 1.5 gigs of memory. Seems quite excessive.

sbabeal avatar Aug 18 '17 15:08 sbabeal

92% chunk asset optimization <--- Last few GCs --->

[3933:0x2a7d710] 355436 ms: Mark-sweep 1395.8 (1546.4) -> 1395.3 (1546.4) MB, 975.3 / 0.0 ms allocation failure GC in old space requested [3933:0x2a7d710] 356584 ms: Mark-sweep 1395.3 (1546.4) -> 1395.3 (1546.9) MB, 976.0 / 0.0 ms allocation failure GC in old space requested [3933:0x2a7d710] 357953 ms: Mark-sweep 1395.3 (1546.9) -> 1395.3 (1539.9) MB, 1195.7 / 0.0 ms last resort [3933:0x2a7d710] 359143 ms: Mark-sweep 1395.3 (1539.9) -> 1395.3 (1539.9) MB, 1188.8 / 0.0 ms last resort

<--- JS stacktrace --->

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

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: node::Abort() [@angular/cli] 2: 0x12b717c [@angular/cli] 3: v8::Utils::ReportOOMFailure(char const*, bool) [@angular/cli] 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [@angular/cli] 5: v8::internal::Factory::NewCode(v8::internal::CodeDesc const&, unsigned int, v8::internal::Handlev8::internal::Object, bool, bool, int, bool) [@angular/cli] 6: v8::internal::CodeGenerator::MakeCodeEpilogue(v8::internal::MacroAssembler*, v8::internal::EhFrameWriter*, v8::internal::CompilationInfo*, v8::internal::Handlev8::internal::Object) [@angular/cli] 7: v8::internal::FullCodeGenerator::MakeCode(v8::internal::CompilationInfo*, unsigned long) [@angular/cli] 8: v8::internal::FullCodegenCompilationJob::ExecuteJobImpl() [@angular/cli] 9: v8::internal::CompilationJob::ExecuteJob() [@angular/cli] 10: 0xd577dc [@angular/cli] 11: 0xd59faa [@angular/cli] 12: 0xd5b7a9 [@angular/cli] 13: v8::internal::Compiler::Compile(v8::internal::Handlev8::internal::JSFunction, v8::internal::Compiler::ClearExceptionFlag) [@angular/cli] 14: v8::internal::Runtime_CompileLazy(int, v8::internal::Object**, v8::internal::Isolate*) [@angular/cli] 15: 0x3e6ef29040c7 Aborted (core dumped)

phamtuananh791994 avatar Aug 21 '17 06:08 phamtuananh791994

We seem to face the error even on allocating 12GB of memory for the build. Is there any way to debug what is causing the memory leak? also, could this be solved by implementing lazy loading of modules, since this is during build time?

Commands used: 1.node --max_old_space_size=12000 ./node_modules/@angular/cli/bin/ng build --prod --aot --env=prod 2. updated the ng to accept the --max_old_space_size parameter

The build is working only with --sourcemap=false and --aot=false; but this is causing memory leaks and page load issues in the browser due to huge application size.

StackTrace:- [INFO] [INFO] <--- Last few GCs ---> [INFO] [INFO] [11944:0x2710b70] 898052 ms: Mark-sweep 11998.2 (13529.5) -> 11998.2 (13529.5) MB, 4991.9 / 0.0 ms allocation failure GC in old space requested [INFO] [11944:0x2710b70] 903445 ms: Mark-sweep 11998.2 (13529.5) -> 11998.1 (13434.0) MB, 5366.9 / 0.0 ms last resort [INFO] [11944:0x2710b70] 908749 ms: Mark-sweep 11998.1 (13434.0) -> 11998.1 (13378.0) MB, 5266.9 / 0.0 ms last resort [INFO] [INFO] [INFO] <--- JS stacktrace ---> [INFO] [INFO] ==== JS stack trace ========================================= [INFO] [INFO] Security context: 0x2b8a8b3a66a1 [INFO] 1: DoJoin(aka DoJoin) [native array.js:~97] [pc=0x168f8ea1398a](this=0x38d692102311 ,q=0x39d30737fbe1 <JS Array[2905]>,r=2905,F=0x38d6921023b1 ,B=0x38d692157ff9 <String[1]: >,A=0x38d692102421 ) [INFO] 2: Join(aka Join) [native array.js:~122] [pc=0x168f8e9b25e2](this=0x38d692102311 ,q=0x39d30737fbe1 <JS Array[2905]>,r=2905,B=0x38d692157ff9 <String[1]: >,A=0x... [INFO]

linkevin777 avatar Aug 21 '17 07:08 linkevin777

I don't know if it can help someone but we had the same problem after upgrading to version 1.3.0. And we had others problem too : slow compilation and bad names for generated chunk.

All our problems were solved with this : https://github.com/angular/angular-cli/pull/7338

loic49 avatar Aug 21 '17 09:08 loic49

FTR, for one project, we abandoned Angular because of this. No amount of --max_old_space_size helps anymore.

Great work alienating enterprise users.

ghost avatar Aug 22 '17 12:08 ghost

There's always an option to eject the app.

Masquer avatar Aug 22 '17 13:08 Masquer

What do you mean by ejecting the application?

On Tue, Aug 22, 2017 at 5:12 PM, Arseny E. Naryshkin < [email protected]> wrote:

There's always an option to eject the app.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/5618#issuecomment-324021572, or mute the thread https://github.com/notifications/unsubscribe-auth/ADuwsXkLSkoHTg9-AMuUESUfM2sCLVbOks5satPRgaJpZM4Mn-Nn .

linkevin777 avatar Aug 22 '17 14:08 linkevin777

Still a problem for us even after CLI upgrade to 1.3.1 and @angular upgrade to 4.3.5

tried typescript 2.5 but run into some compilation problems. we're on 2.4.2.

edited:

On our local machines or our gitlab runner instances we can't reproduce the problem. For some reason the build only fails on our jenkins box. The only difference I've been able to gather is jenkins is on a slightly older version of node/npm version:

v6.9.1
3.10.8

instead of

v6.11.0
3.10.10

fixed by changing our production npm build script to:

"build-prod": "node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng build --prod --aot",

Not so much a fix as it is a brute force work-around, but it'll at least buy time while we figure out what's eating up all the heap space.

randyaa avatar Aug 22 '17 15:08 randyaa

@Masquer how would eject help? It's actually a webpack problem.

ghost avatar Aug 22 '17 17:08 ghost

@notsonotso oh, it will not then.

my app, however, fails to compile by ng with 'heap out of memory' error, but does as ejected.

Masquer avatar Aug 22 '17 17:08 Masquer

Here's a related, solved issue in angular-seed. I hope it helps someone.

mgechev avatar Aug 24 '17 18:08 mgechev

No amount of setting max-old-space-size helped on our project. However, as we only build AOT in prod, we have temporarily disabled sourcemaps which seemed to fix it for us on our CI server.

mattem avatar Aug 24 '17 19:08 mattem

Like @mattem I have not been able to get the max-old-space-size to work either. I do not if I am fully setting it up right though since the build process I have uses webpack with "ngtools/webpack".

timwright35 avatar Aug 24 '17 20:08 timwright35

Same issue, but we've found a more reliable workaround to increase the memory limit.

Since Node 8.x, you can set a NODE_OPTIONS environment variable, and pass in the --old-max-space-size flag. We currently need 6GB.

E.g. on Windows CMD: set NODE_OPTIONS=--old-max-space-size=6411 before running the AOT build.

I wonder if there is a child process that doesn't get the arguments that are set on the node executable.

bgever avatar Aug 25 '17 04:08 bgever

I know, and trust me, I'm just as much struggling with this. Not fun when the build server starts to run out of memory. It's a serious problem that should be addressed. This workaround just helped me to limp along a little farther, just like turning off the source maps. Your mileage may vary.

bgever avatar Aug 25 '17 06:08 bgever

Hello. this problem is serious.

On my computer I was limiting to 8Gb and the ng build was working fine. (with old-max-space-size)

But now I've got a big big project and believe me, out of memory.

I have a code generator that builds everything perfectly. but the new project has more than 200 entities. The creation of modules and lazy loading depends on the settings made by the client and their entities.

I would not want to separate in several applications because there is a lot of dependency. (200 services, 600 components)

The project runs fine with ng serve, but not with ng build. I rented a virtual machine with 28Gb ram and believe me, did the build using 27Gb of RAM. The service node occupied exactly 27Gb.

(I put the limit of 28gb)

Everything works fine with ng serve.

can anyone know to tell me if just separating into several lazy loading modules it will need less memory to compile with ng build? or it works only to separate the JS?

I can not rent a virtual machine whenever I need to run ng build. And the effort of change will be very high. So I needed to be sure of what to do.

Thanks.

eduardotolino avatar Aug 31 '17 22:08 eduardotolino

@Tolitech Instead of depending on having enough RAM for this to build, just add swap to your machine. The build will be a little slower, but you don't need to be renting machines with 28 GB of RAM.

I too want this to be resolved ASAP, but adding swap space gets around the memory issue.

joecot avatar Aug 31 '17 23:08 joecot

@joecot Thank you. I'll try to do this and see if it works. If it works, I do not see any problems if it takes 8 hours. I leave a night job for publication without problems.

That was a good idea. I'll try.

Thank you.

eduardotolino avatar Aug 31 '17 23:08 eduardotolino

@joecot @notsonotso It worked. I configured 32Gb virtual memory (swap), --max_old_space_size and finally worked. It used the 28gb (It's too high), but it worked. Thank you very much for the idea, @joecot

eduardotolino avatar Sep 01 '17 02:09 eduardotolino