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.
Not sure if this is just my experience or not, but in cases where I had template errors (used a non-public variable in template, used the wrong type for an @Input(), etc.) I would see the memory usage climb really high, and then crash the build. My suggestion is to make extra sure that there are no template errors. This can be done by invoking ngc directly on the project. Not sure if that will help, just sharing my experience.
Hey all,
I've been running a bunch of memory profiles on projects to try and figure out what the cause is. I'm comparing projects using CLI 1.5.0 + Angular 5.0.0 + Typescript 2.4.2 against CLI 1.4.9 + Angular 4.4.6 + Typescript 2.3.2. Special thanks to everyone that provided repros, that helps a lot.
@magemello I'm having some trouble building your project. It seems to be a couple of missing style variables (like $alfresco-typography). I assume I have to build these before the app. Can you tell me what the missing steps are? This is what I did:
git clone https://github.com/Alfresco/alfresco-ng2-components
cd alfresco-ng2-components
cd demo-shell-ng2
git checkout development
npm install
npm run ng -- build --prod --build-optimizer # errors on Undefined variable: "$alfresco-typography".
@seanlandsman I could not reproduce with the repository you linked. I got the same results with 1.4.9 as I did with 1.5.0. Builds seemed to finish in roughly the same amount of time, and consume a maximum of 500 MB of memory. Bundle sizes were also the same (more on that in a second). Can you think of anything I might be missing? I'm on Windows 10, node 8.9.1.
@teabagp using your repro (https://github.com/angular/angular-cli/issues/8457#issuecomment-344325608) I saw both a bundle size increase (500kB to 4MB) in 1.5.0 and a memory usage increase (from 580 to 1080 MB). This is the only community repro I really have at the moment, if someone else has a project I can look at please let me know.
The last repro is specially relevant because it shows this problem even in a small project. Those of you that ran into memory problems that went away after increasing the memory limit, can you tell me if your bundles also got significantly bigger?
Based on @teabagp's repro, this is what I can currently say:
- this seems to be related to the library used (
devextremeanddevextreme-angular), - memory usage seems normal up until the "92% asset optimization" phase, then dramatically increases,
- switching build optimizer off doesn't change this,
- switching to the old uglify doesn't change this,
- I don't think it's directly related to the new TS compiler since that runs at the
10% building modulesphase and memory usage seems normal then.
At the moment I don't think the 1.5 memory usage increase is directly related to either Build Optimizer or Sourcemaps. Both of these increase memory usage by virtue of doing more thing. So bigger projects should indeed use more memory. We should still keep this under control but for the 1.5 specific problem it doesn't seem to be the root cause.
@filipesilva
Those of you that ran into memory problems that went away after increasing the memory limit, can you tell me if your bundles also got significantly bigger?
Yes, main bundle increased by roughly 4MB. Sadly the project isn't open source, but it's pretty complex anyway the other project is a better example to try diagnose I think.
Hi @filipesilva,
sorry for not providing you more information on how to run it correctly:
cd scripts
./update-version.sh -v 2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432
cd ..
cd demo-shell-ng2
npm install
npm run ng -- build --prod --build-optimizer
> [email protected] ng /Users/mromano/dev/alfresco-ng2-components/demo-shell-ng2
> ng "build" "--prod" "--build-optimizer"
92% chunk asset optimization
@magemello I'm on windows and unfortunately that script doesn't seem to be very windows friendly even with gitbash...
kamik@T460p MINGW64 /d/sandbox/memory/alfresco-1.4/scripts (development)
$ ./update-version.sh -v 2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432
====== New version 2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 =====
====== UPDATE COMPONENTS ======
====== clean lock file ng2-alfresco-core ======
rm: cannot remove '/d/sandbox/memory/alfresco-1.4/scripts/../ng2-components/ng2-alfresco-core/package-lock.json': No such file or directory
====== UPDATE COMPONENT ng2-alfresco-core ======
====== UPDATE PACKAGE VERSION of to 2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 version in all the package.json ======
sed: can't read s/"version": "[0-9]\.[0-9]\.[0-9]"/"version": "2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
====== UPDATE DEPENDENCY VERSION of ng2-alfresco-core to ~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 in ng2-alfresco-core======
sed: can't read s/"ng2-alfresco-core": "[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-core": "2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
sed: can't read s/"ng2-alfresco-core": "~[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-core": "~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
====== UPDATE DEPENDENCY VERSION of ng2-alfresco-datatable to ~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 in ng2-alfresco-core======
sed: can't read s/"ng2-alfresco-datatable": "[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-datatable": "2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
sed: can't read s/"ng2-alfresco-datatable": "~[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-datatable": "~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
====== UPDATE DEPENDENCY VERSION of ng2-alfresco-upload to ~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 in ng2-alfresco-core======
sed: can't read s/"ng2-alfresco-upload": "[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-upload": "2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
sed: can't read s/"ng2-alfresco-upload": "~[0-9]\.[0-9]\.[0-9]"/"ng2-alfresco-upload": "~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
====== UPDATE DEPENDENCY VERSION of ng2-activiti-diagrams to ~2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432 in ng2-alfresco-core======
sed: can't read s/"ng2-activiti-diagrams": "[0-9]\.[0-9]\.[0-9]"/"ng2-activiti-diagrams": "2.0.0-6a24c6ef754f24212bdd7120d5cdbe47b83c3432"/g: No such file or directory
Is there something I can do even if hacky to get around the need for it? Ideally without removing the global styles as I'm also investigating if style processing is using a lot of memory.
@hanvyj thanks for confirming that!
@filipesilva We started experiencing this pretty significantly in CLI 1.3 & it got worse with 1.4 and so we downgraded to 1.2.6 and haven't had any problems since. we're still running ng 4.4.6 with cli 1.2.6 and it seems fine with default settings.
I don't have a ton of time to dig into this right now so i'm sorry this won't be super helpful but just wanted to let you know where the version numbers went wrong.
We are locked into the following specific versions (along with material 2.0.0-beta.12). if we change JUST cli to 1.3 or 1.4 (and presumably 1.5) we see the issue:
"dependencies": {
"@angular/animations": "4.4.6",
"@angular/cdk": "2.0.0-beta.12",
"@angular/common": "4.4.6",
"@angular/compiler": "4.4.6",
"@angular/core": "4.4.6",
"@angular/forms": "4.4.6",
"@angular/http": "4.4.6",
"@angular/material": "2.0.0-beta.12",
"@angular/platform-browser": "4.4.6",
"@angular/platform-browser-dynamic": "4.4.6",
"@angular/router": "4.4.6",
"@ngrx/store": "4.0.3",
"@ngrx/store-devtools": "4.0.0",
"core-js": "2.4.1",
"dexie": "1.5.1",
"font-awesome": "4.7.0",
"fs-extra": "2.0.0",
"hammerjs": "2.0.8",
"lodash": "4.5.0",
"lscache": "1.0.5",
"moment": "2.17.1",
"net": "1.0.2",
"ng-diff-match-patch": "2.0.6",
"ng2-file-upload": "1.2.0",
"ng2-select": "1.2.0",
"ngx-infinite-scroll": "0.5.1",
"normalizr": "2.1.0",
"primeng": "4.1.3",
"rxjs": "5.4.3",
"stompjs": "2.3.3",
"zone.js": "0.8.14"
},
"devDependencies": {
"@angular/cli": "1.2.6",
"@angular/compiler-cli": "4.4.6",
"@compodoc/compodoc": "1.0.3",
"@types/jasmine": "2.5.53",
"@types/jasminewd2": "2.0.2",
"@types/lodash": "4.14.65",
"@types/lscache": "1.0.27",
"@types/node": "6.0.60",
"@types/normalizr": "2.0.16",
"agentkeepalive": "^3.3.0",
"codelyzer": "3.1.2",
"jasmine-core": "2.6.2",
"jasmine-spec-reporter": "4.1.0",
"karma": "1.7.0",
"karma-chrome-launcher": "2.1.1",
"karma-cli": "1.0.1",
"karma-coverage-istanbul-reporter": "1.2.1",
"karma-firefox-launcher": "1.0.0",
"karma-jasmine": "1.1.0",
"karma-jasmine-html-reporter": "0.2.2",
"karma-phantomjs-launcher": "1.0.2",
"node-sass": "4.5.3",
"phantomjs-prebuilt": "2.1.7",
"protractor": "5.1.2",
"suitcss-components-grid": "3.0.3",
"suitcss-utils-size": "1.1.0",
"ts-node": "3.2.0",
"tslint": "5.3.2",
"typescript": "2.4.2"
}
We were hoping to upgrade soon so glad to see some focus on the issue. Thanks so much!
I have executed it for you:
git checkout dev-filipesilva
cd demo-shell-ng2
npm install
npm run ng -- build --prod --build-optimizer
@filipesilva
I've just tried on a clean install and it still fails for me. I'm on OSX Sierra and am running node v7.7.3
I also note that if I run ng build --prod --build-optimizer=false it will run successfully repeatedly, whereas running just ng build --prod will fail most of the time.
I'm not sure what I can add here - is there some sort of profiling I can run on my side and provide the output?
thanks
this is also affecting me, using angular 5 and cli 1.5, ive had to switch off the build optimizer to get my builds to pass without a out of memory error :(
@seanlandsman do the bundle sizes remain the same?
@filipesilva
With "node --stack_size=4048 node_modules/.bin/ng build --prod" I get:
1.4K 14 Nov 20:43 inline.aa760cefdeaeea9e038f.bundle.js 835K 14 Nov 20:43 main.05e5301d42766024fc03.bundle.js 60K 14 Nov 20:43 polyfills.e6475d2787bb3154d59c.bundle.js 48K 14 Nov 20:43 styles.9b2fb464224d0a79781d.bundle.css
With "./node_modules/.bin/ng build --prod --build-optimizer=false" I get:
1.4K 14 Nov 20:45 inline.d7125ae6ed51e3e3af81.bundle.js 4.9K 14 Nov 20:45 main.47d0056e07ee5cb9c483.bundle.js 60K 14 Nov 20:45 polyfills.ad37cd45a71cb38eee76.bundle.js 48K 14 Nov 20:45 styles.9b2fb464224d0a79781d.bundle.css 951K 14 Nov 20:45 vendor.799da157f41ee457e4bd.bundle.js
Interestingly you can see the main.xx.js got much bigger with in the first run, but the vendor file disappeared. This might be expected?
One further bit of information - I just tried this on an older Mac running El Capitan (node version v7.7.3) that I've not used for a while. When I run "node_modules/.bin/ng build --prod" it runs just fine, repeatedly, and much faster than on my current machine. Hopefully this is helpful.
thanks
@filipesilva our build also got increased by 3MB or 4MB like the last example you said, taking a long time after the 92%, and we also use the devextreme libraries. Can't share the project repo unfortunately but I could provide our package.json if neeeded.
@filipesilva try to reduce the stack_size to reproduce the @seanlandsman case.
node --stack_size=512 node_modules/.bin/ng build --prod gets me that error on my mac.
@filipesilva What version of the CLI and Angular are you on? 1.5 and 5.0.0 respectivley Does this only happen on ng build --prod? Only when the --aot flag is use. Do you still get the memory error with these build commands: ng build --build-optimizer=false no ng build --aot yes ng build --aot --build-optimizer yes If you have a project that I can look at, can you link it please? Sorry, but our application is private per our corporate requirements.
@jmcclanahan @filipesilva
ng serve : SUCCESS ng build : SUCCESS
ng build --prod : FAIL ng build --prod --build-optimizer=false : FAIL ng build --aot : FAIL ng build --aot --build-optimizer : FAIL
output as per #5618 (comment)
It also takes several 'minutes' hung at 92% before producing an error.
Is there any workaround to bypass it? It is not working with the --max_old_space_size=8192
I have the same problem. I also recognized that the size of my application with ng serve drastically increased after the update to angular 5. With angular 4 the vendor.bundle.js created by ng serve used to be 10MB and with angular 5 it is now 30MB.
ng serve -> works ng build -> works ng build --prod -> fails ng build --prod --build-optimizer=false -> fails ng build --aot -> fails ng build --aot --build-optimizer=false -> fails
@filipesilva we all could also help debugging the webpack build, what tool are you using to debug it? I was trying like that, but I'm not sure is the best way:
node-nightly --inspect-brk ./node_modules/@angular/cli/bin/ng build --prod
If you give me more instructions here or on Gitter I will be more than happy to help.
Heya, using the repros you all provided I believe I've found the main problem with 1.5. It has to do with how the new pipeline moved decorator removal into Build Optimizer.
For those interested in a more detailed explanation, this is what happens:
- When you build with
--aot, we use TypeScript transforms in Build Optimizer to remove Angular decorators (stuff like@Componentand@NgModule) from both your app and libraries (they aren't needed after AOT). - In 1.4 we didn't use Build Optimizer for that, we used manual removal on your app only.
- Due to a bug, decorators are removed but their imports stay in the code.
- These imports make some third party libs be wholly included, instead of only partially included.
- You can check this yourself by comparing
ng build --aotwhen using CLI1.4.9+NG4 versus CLI1.5.0+NG5. - In the
devextremelib case, there was almost 9megs of extra code included. - Extra code means a lot more work for Uglify, which makes it consume a lot more memory.
There might be more factors contributing to the excessive memory usage but currently this is the approach I'm following as it's the most promising one. I hope to have a PR with a fix soon.
@filipesilva awesome news! Thanks for sharing your progress - very much appreciated!
Some of us are experiencing this in non-latest-version setups and others all the way back in cli 1.2 and 1.3 setups. Do you believe this is just a natural limitation and expected behavior due to the size of the projects/dependencies? Is the default <2GB RAM allocation just naturally too small for compiling bigger projects?