Angular 7/8 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
π Bug report
Command (mark with an x)
- [ ] new
- [X] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
Description
With the last merge project build process is not working. I get failure that is pasted below. Tried to change --max-old-space-size=4096 still not working. Any suggestions what this can be ?
π¬ Minimal Reproduction
run command ng build --prod
in angular.json file we have
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
},
π₯ Exception or Error
Here is the log file.
{ Error: Command failed: ng build --prod --configuration=config --output-path=test --base-href=/test/
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x8dbaa0 node::Abort() [ng build --prod --configuration=config --output-path=test --base-href=/test/]
2: 0x8dbaec [ng build --prod --configuration=config --output-path=test --base-href=/test/]
3: 0xad83de v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
4: 0xad8614 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
5: 0xec5c42 [ng build --prod --configuration=config --output-path=test --base-href=/test/]
6: 0xec5d48 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
7: 0xed1e22 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
8: 0xed2754 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
9: 0xed53c1 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
10: 0xe9d636 [ng build --prod --configuration=config --output-path=test --base-href=/test/]
11: 0xeafee7 v8::internal::Factory::NewLoadHandler(int) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
12: 0xf2f4db v8::internal::LoadHandler::LoadFromPrototype(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::Handle<v8::internal::Smi>, v8::internal::MaybeHandle<v8::internal::Object>, v8::internal::MaybeHandle<v8::internal::Object>) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
13: 0xf36d1f v8::internal::LoadIC::ComputeHandler(v8::internal::LookupIterator*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
14: 0xf3d94c v8::internal::LoadIC::UpdateCaches(v8::internal::LookupIterator*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
15: 0xf3dffc v8::internal::LoadIC::Load(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
16: 0xf42935 v8::internal::Runtime_LoadIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
17: 0x235e3925be1d
π Your Environment
Angular CLI: 7.3.1
Node: 10.15.1
OS: linux x64
Angular: 7.2.6
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.2
@angular-devkit/build-angular 0.13.2
@angular-devkit/build-optimizer 0.13.2
@angular-devkit/build-webpack 0.13.2
@angular-devkit/core 7.3.2
@angular-devkit/schematics 7.3.1
@angular/cdk 7.3.3
@angular/cli 7.3.1
@ngtools/webpack 7.3.2
@schematics/angular 7.3.1
@schematics/update 0.13.1
rxjs 6.4.0
typescript 3.1.6
webpack 4.29.0
I have the following environment:
Node 12.9.1 Angular 8.25 (from ng upgrade)
@angular-devkit/build-angular and build-ng-packagr of 0.803.3 @angular/cli of 8.3.3
When running ng build --prod, and watching the node process, I see similar behavior in the memory usage as described above, where it barely eclipses 2 GB of usage. However, at one point it just fails with the error message below. Seems to be crashing with webpack's usage of the acorn library.
I have a very large SPA that is utilizing sass, and i'm using node-sass instead of dart.
I'm seeing the following stack trace when trying to run ng build --prod:
ng build --prod 90% chunk assets processing <--- Last few GCs --->
[32136:00000223FBDD0BD0] 321271 ms: Mark-sweep 1906.3 (2091.5) -> 1906.0 (2093.0) MB, 1306.9 / 0.0 ms (average mu = 0.187, current mu = 0.124) allocation failure GC in old space requested [32136:00000223FBDD0BD0] 321985 ms: Mark-sweep 1912.7 (2093.0) -> 1910.5 (2093.0) MB, 656.7 / 0.1 ms (average mu = 0.152, current mu = 0.081) allocation failure GC in old space requested
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 00007FF779B1224D]
1: StubFrame [pc: 00007FF779B75575]
Security context: 0x022679540919
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 00007FF778F5F0DF napi_wrap+121039 2: 00007FF778F051D6 v8::base::CPU::has_sse+34470 3: 00007FF778F05E96 v8::base::CPU::has_sse+37734 4: 00007FF7796F89AE v8::Isolate::ReportExternalAllocationLimitReached+94 5: 00007FF7796E0951 v8::SharedArrayBuffer::Externalize+833 6: 00007FF7795AEC8C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436 7: 00007FF7795B812F v8::internal::Heap::ProtectUnprotectedMemoryChunks+1279 8: 00007FF7795B6614 v8::internal::Heap::PageFlagsAreConsistent+3204 9: 00007FF7795AC263 v8::internal::Heap::CollectGarbage+1235 10: 00007FF7795AAB04 v8::internal::Heap::AddRetainedMap+2356 11: 00007FF7795C7684 v8::internal::Factory::NewByteArray+100 12: 00007FF7796115D7 v8::internal::BasicBlockProfiler::Data::ResetCounts+8647 13: 00007FF7799194AC v8::internal::compiler::CodeGenerator::GenerateDeoptimizationData+124 14: 00007FF779919247 v8::internal::compiler::CodeGenerator::FinalizeCode+103 15: 00007FF77994FAEA v8::internal::compiler::LiveRange::End+746 16: 00007FF77995011F v8::internal::compiler::LiveRange::End+2335 17: 00007FF77964F0FA v8::internal::Compiler::FinalizeBackgroundCompileTask+922 18: 00007FF77964F583 v8::internal::Compiler::FinalizeOptimizedCompilationJob+931 19: 00007FF779639228 v8::internal::OptimizingCompileDispatcher::InstallOptimizedFunctions+600 20: 00007FF779605CCA v8::internal::StackGuard::HandleInterrupts+1770 21: 00007FF779339432 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+5138 22: 00007FF779B1224D v8::internal::SetupIsolateDelegate::SetupHeap+575565 23: 00007FF779B75575 v8::internal::SetupIsolateDelegate::SetupHeap+981877 24: 00007FF779A8FCFC v8::internal::SetupIsolateDelegate::SetupHeap+41724 25: 0000039B9B7A5C28
Wanted to add a bit more of information. ng build seems to work fine. only when using the --prod flag is where the heap issue happens. Here's my prod configuration from the angular.json:
"production": { "fileReplacements": [{ "replace": "apps/standard/src/environments/environment.ts", "with": "apps/standard/src/environments/environment.prod.ts" }], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true, "vendorSourceMap": false },
Thanks a lot @filipesilva for the exhaustive benchmark. Installing node-sass didn't pull us out but upgrading angular to v8.2.6 did the trick.
Still, isn't it weird that most of people reporting the JavaScript heap out of memory error get stuck at 92% precisely ? I would blame the Terser optimisation process for the sudden memory peak.
Is there any way to resolve this issue without updating node to current from LTS? In my project build gets generated in server. I can't ask all servers to update node.
@akvaliya node 12 will be LTS in less than a month. I can't see how a bug this intricate & wide reaching makes sense to tackle using a version that's about to be become 'semi-legacy'. https://nodejs.org/en/about/releases/
@tomgruszowski Yes. But i can't tell client to update all his servers.
@akvaliya Why are your application servers building the front-end?
@benelliott Well i just push code to repo & testers from client team will pull & redeploy with build.
I don't this should be the point that why are we building fron-ent in server. Some client wants code to auto redeploy after commit. Like using jenkins.
for me the solution was editing my tsconfig (ionic4 and angular8)
i was playing around adding properties to tsconfig, towards the end i enabled source maps for my prod build inside angular.json.
i had to remove the sourceMap: true and sourceBase: "/" from tsconfig , after that --prod works like a charm
"scripts": {
"start": "node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng serve",
"start:prod": "node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng serve --prod",
}
Above config solves for me
For us upgrading from Angular 8.2.0 to 8.2.8 did the trick and we no longer got the error. While allocating more memory did not work for us.
@filipesilva just install Node 12.8 solved the issue . thanks.
I have the same issue with 12.8 too.
@filipesilva just install Node 12.8 solved the issue . thanks.
Was getting with debug and Node >12.8 fix for me
I have a custom project not using Angular but built with Webpack, and getting this error when I build with Webpack in production mode. I tried --max_old_space_size=8192 but no luck (I guess I need more).
I wonder what's actually the problem though, I bet differing frameworks/libs/apps may have similar Webpack configs that can cause this.
I had the same issue and i did these steps:
ng update @angular/cli @angular/core- update node to v12.11.1
works fine with
--max_old_space_size=8192"@angular/core": "8.2.8", "@angular/cli": "^8.3.6"
The specific cause of this error for me (in my non-angular project) is the use of uglifyjs-webpack-plugin. It uses a ton a memory. For now I disabled minification in production, and will have to figure out how to fix it later.
I disabled minification in production
π¨
This has been working for me for dev builds. I haven't seen this issue with production builds, yet.
"build:highmem": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build",
I only started to need this after updating Angular and CLI from 8.0.0 to the latest 8.2.8 and 8.3.6. Here's the PR where this was changed: https://github.com/angular/material.angular.io/pull/641
The specific cause of this error for me (in my non-angular project) is the use of
uglifyjs-webpack-plugin. It uses a ton a memory. For now I disabled minification in production, and will have to figure out how to fix it later.
This is probably related to source maps. Try setting sourceMap to false in tsconfig.json (or, for those with angular projects, in angular.json).
A Note For Angular/Windows/Docker Users
If you are trying to run 'npm run build' or something of the sort inside your Docker file and you are getting this error, as mention above, you want to use the following run command instead, increase Node.js' access to memory.
node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod
In addition, however, Docker is running a Linux VM in the background that will also have memory constraints that need to be addressed.
Navigate to 'C:\Users<profile name>\AppData\Roaming\Docker' where profile name is the named of your logged in profile. There you will find a 'settings.json' file. Open it and adjust the 'memoryMiB' value to also be 8192. You will need to restart for the changes to take effect.
WARNING
Increasing the VM memory can have a drastic impact on your machine performance, so ensure that you have enough physical memory to do this before trying. If this solution works for you, you should then try to roll back the values as much as you can, by doing a process something like, halving the value initially, testing, halving it again if it works, or adding half of the new value back if it doesn't etc. This will help you minimize the impact on your machine.
You will also want to disable Docker as a start up program for Windows 10. Simply search "startup apps" in the task bar and select the corresponding option that appears. This way, you wont have a VM you are not using bogging down your system.
for me the solution was editing my tsconfig (ionic4 and angular8)
i was playing around adding properties to tsconfig, towards the end i enabled source maps for my prod build inside
angular.json.i had to remove the
sourceMap: trueandsourceBase: "/"from tsconfig , after that --prod works like a charm
Just removing sourceMap worked for me. Frustrating....