Reputation: 10386
AoT compilation resulting in " JavaScript heap out of memory" error
I am trying to compile my project by following the AoT guide available on angular.io
Below is my typesccript configuration file for aot
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"noEmitOnError": true,
"removeComments": true,
"sourceMap": false,
"target": "es5",
"outDir": "../content/app/"
},
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
I am compiling using below command:
D:\MyProject\node_modules\.bin>ngc -p ../../app/tsconfig-aot.json
It takes times in compilation and ends up with below errors:
<--- Last few GCs --->
215225 ms: Mark-sweep 1327.9 (1434.3) -> 1316.1 (1434.3) MB, 1233.5 / 0.0 ms [allocation failure] [GC in old space requested].
216639 ms: Mark-sweep 1316.1 (1434.3) -> 1316.1 (1434.3) MB, 1413.5 / 0.0 ms [allocation failure] [GC in old space requested].
217985 ms: Mark-sweep 1316.1 (1434.3) -> 1320.4 (1403.3) MB, 1346.7 / 0.0 ms [last resort gc].
219337 ms: Mark-sweep 1320.4 (1403.3) -> 1324.6 (1403.3) MB, 1351.5 / 0.0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 00000023CEBCFB61 <JS Object>
1: copyProperties [D:\MyProject\node_modules\typescript\lib\typescript.js:1462] [pc=000001870A3EC95C] (this=000000AAE12C6A41 <an Object with map 00000279F6259FA1>,source=000002E70921E389 <an Object with map 00000279F62D8539>,target=000002ED3C5C7691 <an Object with map 000000D6F40CD1C9>)
2: objectTypeRelatedTo [D:\MyProject\node_modules\typescript\lib\typescript.js:...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
D:\MyProject\node_modules\.bin>
The project I am trying to compile has 467 files.
Upvotes: 1
Views: 1554
Reputation: 1
node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng [command]
e.g node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng serve
worked for me on my windows machine
Upvotes: 0
Reputation: 443
If you still have this issue on windows after trying the accepted answer you can use the following workaround (@angular/compiler-cli 2.2.1):
node --max-old-space-size=8192 node_modules/@angular/compiler-cli/src/main.js -p tsconfig-aot.json
This might not work with future updates as all the above does is point to the actual js file if you interpret ./node_modules/.bin/ngc.
Upvotes: 1
Reputation: 41
Here is how I was FINALLY able to compile my project with ngc (after a month of looking everywhere and failing each time)
This doesn't work on windows for some reason, but it worked on a mac machine
first compile with main.ts in JIT mode
./node_modules/.bin/ngc -p tsconfig-aot.json
then change main.ts to AoT mode and run
node --max-old-space-size=8192 ./node_modules/.bin/ngc -p tsconfig-aot.json
I literally had to go as far as 8192 (8GB) to not run out of memory (it goes as far as 5-6GB for my project)
then build the rollup bundle
node --max-old-space-size=8192 node_modules/.bin/rollup -c rollup.js
if everything went well, now you should have a production bundle.
A note on Lazy loading - I'm not using lazy loading as if I do, ngc just ignores all the lazy loaded modules and does not generate AoT files for them. I believe lazy loading is supposed to be used only if you're not going to generate a production build and is going to use a module loader like SystemJS or Webpack to load your project
My machine is
Upvotes: 0