AOT build fails due to "JavaScript heap out of memory"
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.
@mast3rd3mon
I have 8 modules with approximately 89 components, 20 services, a number of interfaces and I believe around 52 model classes, failing with or without sourcemaps
@jarodsmk both of my projects are bigger and removing that --sm and also upgrading to the latest deps for ts and cli (and of course ng) solved the js memory error.
Hmm, we're having 60 components, 5 own modules, 47 3rd party modules imported and about 50 services. I consider this to be a really simple app compared to some of the enterprise stuff I've worked with.
What does the --sm flag do @makdeniss ?
@swftvsn source maps. better debugging posibilities, but for prod this is not needed anyways.
@makdeniss hmm interesting, I've tried updating my ts & cli (local on a different branch and global) but didn't change anything
@jarodsmk how are you building the project? I didn't change any npm memory flags btw. All is as default.
@makdeniss
So these fail:
ng b -op aot -aot -sm -v
ng b -op aot -aot -v
But I resorted to:
node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng b -op aot -aot -sm -v
Which worked. Interesting that setting the max_old_space_size to 4gb helped
@jarodsmk it should help, because you are giving in more memory. This is a workaround, but a hacky one.
I ran into a this issue today, but I saw it when attempting to perform a regular ng build. It would stop at 10% and hang until the error occurred. Allocating more memory did allow it to build. I found this issue on typescript (https://github.com/Microsoft/TypeScript/issues/14628) and one of the comments led me to changing the tsconfig.app.json.
tsconfig.app.json
"include": [
"src",
"typedefs"
],
This appears to have fixed the issue. However, the tests will still not work and I have not been able to add anything to the tsconfig.spec.json to get them working.
Environment:
angular-cli: 1.3.1
node: 8.10.0
os: macOS 10.12.6
I've been following this thread for a while, but not sure if this has been mentioned before.
In my case, I'm doing the auto-rebuild stuff. So when I make a modification in my source, it'll rebuild and reload the browser page. The initial startup build always works, but after doing approximately 20 or more modifications and therefore rebuilds, I end up getting the out of memory error.
I have a fairly small project so it isn't the issue that the project itself will build and hit a memory limit. I ran into the memory issue after 10 or more repeated builds were run in TeamCity. Restarting the build agent/server got things back to normal, so it seems that memory is not being cleared after each build and eventually aggregating toward the limit.
@Ericdowney angular-cli 1.3.1 is quite old in this context. A lot has happened since then until 1.7.x. And a lot more is about to happen when they release 6.x. Might want to either switch to 1.7.x now and then use ng update to update to 6.x when that comes out and you also want to migrate to Angular 6.
I wouldn't expect any fixes or help with angular cli version 1.3.1. Maybe version 6 will have similar LTS support but would always assume the latest version to give the best experience.
I previously had this error on Angular 5 with --aot flag and then it went away with the updates to @angular/cli. I have now upgraded to Angular 6 and the issue is back :/. I'm using:
ng serve myapp --prod
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"optimization": true,
"aot": true,
"vendorChunk": false
}
}
And it fails with:
** Angular Live Development Server is listening on localhost: 4200, open your browser on http://localhost:4200/ **
90% chunk assets processing
<--- Last few GCs --->
[19772:000001E2123ECE00] 95613 ms: Mark-sweep 1404.0 (1506.8) -> 1404.0 (1503.8) MB, 1021.6 / 0.0 ms (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1130 ms) last resort GC in old space requested
[19772:000001E2123ECE00] 96624 ms: Mark-sweep 1404.0 (1503.8) -> 1404.0 (1503.8) MB, 1010.8 / 0.0 ms last resort GC in old space requested
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 000000B751F25EE1 <JSObject>
2: declareVarName [c:\dev\FitRockApp\node_modules\acorn\dist\acorn.js:2831] [bytecode=0000036F39CCD189 offset=32](this=0000016629E387F9 <Parser map = 0000000C4C3050C1>,name=0000016629E37629 <String[33]: styles_DietTemplatesPageComponent>)
4: checkLVal [c:\dev\FitRockApp\node_modules\acorn\dist\acorn.js:1749] [bytecode=0000036F39CCC539 offset=325](this=0000016629E387F9 <Parser map = 000...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node_module_register
2: v8::internal::FatalProcessOutOfMemory
3: v8::internal::FatalProcessOutOfMemory
4: v8::internal::Factory::NewTransitionArray
5: v8::internal::EhFrameIterator::DecodeSLeb128
6: v8::internal::JSReceiver::GetOwnPropertyDescriptor
7: v8::internal::JSReceiver::GetOwnPropertyDescriptor
8: v8::internal::JSReceiver::GetOwnPropertyDescriptor
9: v8::internal::JSReceiver::class_name
10: v8::internal::JSReceiver::GetOwnPropertyDescriptor
11: v8::internal::LookupIterator::PrepareTransitionToDataProperty
12: v8::internal::JSReceiver::class_name
13: v8::internal::ParserBase<v8::internal::Parser>::MarkLoopVariableAsAssigned
14: v8::internal::ParserBase<v8::internal::Parser>::MarkLoopVariableAsAssigned
15: 000000DA23D847A1
Any ideas how to fix these? It works fine when aot is disabled.
@Enngage You can replace this line:
ng serve myapp --prod
with one of the following alternatives
Alternative 1: Create this npm script entry:
"ng-high-mem": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng",
You can then in your console write:
npm run ng-high-mem -- serve myapp --prod
Alternative 2: Run this in the console:
node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve myapp --prod
Thank you @Koslun for this complete example. I went with the 1st approach and works well! I'm curious as to why this isn't mentioned anywhere in docs. This seems like any reasonably sized project would have to do anyway.
@Enngage think it's cause the goal is that this is never really supposed to happen. Where having to use this can often also instead be the result of some regression in angular-cli, Webpack or even Node, such as this fixed but unreleased regression in Node: https://github.com/nodejs/node/issues/19769.
Given that this issue itself has the tag FAQ and the traffic it's garnered I agree that it might be worthwhile to add it to some FAQ in the documentation.
Have the same issue and sudo node --max-old-space-size=4096 /usr/local/bin/ionic cordova build android --prod didn't help. I'm using MacOs. Any advice ?
I am following this way: Inspect the bundles and I have the same error:
"devDependencies": {
"@angular/cli": "^1.7.2",
"@angular/compiler-cli": "^5.2.9",
ng build --prod --sourcemaps 92% chunk asset optimization <--- Last few GCs ---> [6360:0000000000398AE0] 1741315 ms: Mark-sweep 1399.2 (1647.1) -> 1398.3 (1648.6) MB, 1257.4 / 0.0 ms allocation failure GC in old space requested [6360:0000000000398AE0] 1742847 ms: Mark-sweep 1398.3 (1648.6) -> 1398.1 (1603.1) MB, 1530.9 / 0.0 ms last resort GC in old space requested [6360:0000000000398AE0] 1744175 ms: Mark-sweep 1398.1 (1603.1) -> 1398.1 (1595.1) MB, 1328.2 / 0.0 ms last resort GC in old space requested <--- JS stacktrace ---> ==== JS stack trace ========================================= Security context: 0000018821D25EE1
1: exec(this=00000156D3AF9AA9 <JSRegExp <String[53]: /** PURE_IMPORTS_START (\S+) PURE_IMPORTS_END */>>,000003A248D4D541 <Very long string[6384078]>) 3: getMatches(aka getMatches) [C:\Users\Utente\Desktop\Workspace\rebus5\node_modules@angular-devkit\build-optimizer\src\purify\purify.js:48] [bytecode=000003F790876209 offset=16](this=000000FCE1C02311 ,str=000003A248D4D54... FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: node_module_register 2: v8::internal::FatalProcessOutOfMemory 3: v8::internal::FatalProcessOutOfMemory 4: v8::internal::Factory::NewRawTwoByteString 5: v8::internal::Smi::SmiPrint 6: v8::internal::AllocationSpaceName 7: v8::internal::RegExpImpl::Exec 8: v8::internal::RegExpImpl::Exec 9: v8::internal::ParserBasev8::internal::Parser::MarkLoopVariableAsAssigned 10: 00000231754047A1
@lippomano Try this. It solved the issue.
My angular version is 5.1.1 Node version : 8 Npm version: 5
I have tried these command for AOT build even though i'm getting this ERROR JavaScript heap out of memory
- ng build --prod
- ng build --aot
- "build": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod",[I have play round the size also]