angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular 7/8 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Open arayik-yervandyan opened this issue 7 years ago β€’ 205 comments

🐞 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

arayik-yervandyan avatar Feb 21 '19 13:02 arayik-yervandyan

well actually we have a project where setting max_old_space_size=8192 worked, but failed with max_old_space_size=4096, however with 8192 the memory usage peaked at around 4-6g btw. we used max_old_space_size=2048 with angular v7.x.

I'm pretty sure there is a leak. Maybe it only happens with build-optimizer?

schmitch avatar Jun 07 '19 14:06 schmitch

Any news? workarounds? Do you guys need more benchmarks?

hugo-marello avatar Jun 07 '19 21:06 hugo-marello

This is also a problem we are facing with https://github.com/vmware/clarity on the master branch right now. We are unable to build and deploy our website because we run out of memory and other SSR issues. To see memory issues, run npm run website:prerender to trigger the website build.

gnomeontherun avatar Jun 07 '19 21:06 gnomeontherun

Heya, I've put up results and methodology in https://github.com/filipesilva/angular-cli-perf-benchmark. Will be adding more projects there next week.

The only extra detail that I have is that in the awesome-angular-workshop project build times almost doubled from CLI 7 to 8. But they didn't on the other projects I tested.

If someone wants to follow the methodology there using the scripts, it should be easy enough. If you find something interesting let me know. If you have a large project that you feel could yield interesting benchmarks, let me know (or add a PR) and I'll add it there.

filipesilva avatar Jun 07 '19 21:06 filipesilva

same issue here on Darwin Miguels-MacBook-Pro.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Dec 20 21:47:19 PST 2018; root:xnu-4570.71.22~1/RELEASE_X86_64 x86_64

angular cli 5.2.11

MVCpp avatar Jun 08 '19 14:06 MVCpp

Build Problem Solved. Suggestion is to change build logic. Instead of angular ng build use node build

arayik-yervandyan avatar Jun 10 '19 07:06 arayik-yervandyan

@arayik-yervandyan I'd like to keep this open, as there's more to this issue than just increasing the memory limit. Some projects are exhibiting disproportionate increases in memory usage, and we also seem to have memory that is not freed on differential loading builds.

filipesilva avatar Jun 10 '19 12:06 filipesilva

@filipesilva sure no problem. Just for the information I can say that in our version we increased memory limit into 4096 or 8192 I didn't remember correctly but the problem still exists and when build was changes from ng build into node build then build process passed successfully.

arayik-yervandyan avatar Jun 10 '19 13:06 arayik-yervandyan

When you say node build, what exactly do you mean?

filipesilva avatar Jun 10 '19 14:06 filipesilva

I think he meant something in the lines of node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng build --prod, so the argument would be passed to node environment. But there are other issues linked to this one, like the huge time and memory spent while compiling.

hugo-marello avatar Jun 10 '19 19:06 hugo-marello

I agree with @hugo-marello , my use case is indeed this one. During a simple ng serve + args, I went through out of memory errors every time it tried to recompiled. The CPU went crazy at 100% for me. I tried the workaround by setting node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng + args and it worked well to me.

I do see bigger times to recompile nevertheless, but it doesn' crash anymore. So, i feel that something is still off by going to the angular 8 (+cli). By the way, i already use node 12.

johanchouquet avatar Jun 11 '19 08:06 johanchouquet

I finished benchmarking the projects I had in https://github.com/filipesilva/angular-cli-perf-benchmark.

When looking at those results, keep this note in mind:

It's important to note that the first build of the CLI version 8 with differential loading and CLI version 7 variants will always use more cores and take more memory. This happens because terser-webpack-plugin uses both parallel processes and a cache that is cleared on reinstalling the node modules. Cache hits don't spawn extra processes nor perform any work.

This is a bit unfortunate and makes the results harder to read. If I repeat this in the future I'll try to find a way of always busting/ignoring the Terser cache. In the results below I am mostly looking at the numbers with cache, but add a single comparison without cache as well.

Below are the results. Outliers are in bold. N10 and N12 means Node 10.16.0 and Node 12.4.0 respectively. CLI7, CLI8 and CLI8DL mean Angular CLI version 7, version 8 and version 8 with differential loading.

Results

cli-eight-project

In N12 going from CLI7 to CLI8 adds ~20mb (5%) to memory usage and ~60mb (7%) to peak memory.

CLI8DL adds ~190mb to both memory and peak memory, and extra 60% build time.

N10 shows similar numbers.

N12 seems to use about 6% extra memory over N10.

Builds without cache in N12 go from 1063mb peak memory usage in CLI 7 to 1191mb in CLI8DL.

super-productivity

In N12 going from CLI7 to CLI8 adds ~60mb (5%) to memory usage but peak memory stays the same.

CLI8DL adds ~200mb to both memory and peak memory, and extra 90% build time.

N10 shows similar numbers.

N12 seems to use about 30% extra memory over N10.

Builds without cache in N12 go from 2886mb peak memory usage in CLI 7 to 3131mb in CLI8DL.

awesome-angular-workshop

In N12 going from CLI7 to CLI8 adds ~410mb (45%) to memory usage and ~300mb (30%) to peak memory**.

CLI8DL adds ~200mb to both memory and peak memory, and extra 150% build time.

N10 shows similar numbers.

N12 seems to use about 25% extra memory over N10.

Builds without cache in N12 go from 2515mb peak memory usage in CLI 7 to 2554mb in CLI8DL.

angular.io

In N12 going from CLI7 to CLI8 adds ~50mb (6%) to memory usage and ~350mb (25%) to peak memory.

CLI8DL adds ~270mb to memory, 100mb to peak memory, and extra 90% build time.

N10 shows similar numbers.

N12 seems to use about 17% extra memory over N10.

Builds without cache in N12 go from 2898mb peak memory usage in CLI 7 to 2243mb in CLI8DL.

spartacus

In N12 going from CLI7 to CLI8 adds ~290mb (32%) to memory usage and ~420mb (25%) to peak memory.

N10 shows 40% extra memory usage and peak memory.

N12 seems to use about 27% extra memory over N10.

Builds without cache in N12 go from 2011mb peak memory usage in CLI 7 to 2326mb in CLI8.

clarity

In N12 going from CLI7 to CLI8 adds ~50mb (2%) to memory usage but peak memory stays the same.

N10 shows similar numbers.

N12 seems to use about 70% extra memory over N10.

Builds without cache in N12 go from 6000mb peak memory usage in CLI 7 to 5000mb in CLI8.

Conclusions

The first build always takes longer and uses more memory due to caching of some build results. On realistic scenarios most builds will invalidate some of the cache and take longer too.

Using differential loading seems to add a ~200mb to memory usage. This value does not change much, which seems to indicate that something that doesn't depend much on project size is being retained in memory between builds.

Node 12 is using between 6% and 70% extra memory than Node 10. Most projects tested showed a ~30% increase.

awesome-angular-workshop suffers a disproportionate increase in memory usage (45%) and build time going (150%) from CLI7 to CLI8DL. This indicates a localized regression.

spartacus shows a higher memory usage difference between CLI7 and CLI8 in Node 10 when compared to Node 12. This indicates Node 12 is managing memory better in some cases.

clarity uses 70% extra memory in Node 12 when compared to Node 10. This indicates that Node 12 can use much more memory in some cases.

filipesilva avatar Jun 11 '19 13:06 filipesilva

If you are on Node 12 and hitting the memory limit, you should try Node 10 instead for now.

We'll look into the retained memory during Differential Loading builds and the outliers like awesome-angular-workshop.

filipesilva avatar Jun 11 '19 13:06 filipesilva

actually im pretty sure that even with "target": "es5" that the memory usage was gone trough the roof. I would not just blame differential loading for that. I would actually blame TerserPlugin which can be highly ineffiecent and actually is since angular 8.

schmitch avatar Jun 11 '19 14:06 schmitch

@schmitch as far as I can tell DL accounts for an almost static amount of extra memory (~200ish). So it's not great but also not a big deal in most cases.

Regarding TerserPlugin, you can disable it with --optimization=false. You can set this option on the prod config in angular.json too. But cases where you turn it off are cases where you'd just use non prod mode instead really.

filipesilva avatar Jun 11 '19 15:06 filipesilva

While Node 12 will attempt to automatically configure the --max_old_space_size option, the V8 method used to do so will cap the value at 2048MB.

clydin avatar Jun 11 '19 15:06 clydin

Reported the new 2048MB default maximum in https://github.com/nodejs/node/issues/28202.

filipesilva avatar Jun 13 '19 10:06 filipesilva

Reported Node 12 using more memory in https://github.com/nodejs/node/issues/28205.

filipesilva avatar Jun 13 '19 14:06 filipesilva

Ran into this today when trying to enable source maps for my production build.

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0x8dc510 node::Abort() [ng build --prod]

Senneseph avatar Jun 13 '19 19:06 Senneseph

I had this error; turns out it was because I had left a backup node_modules folder in my project directory. Deleting it fixed the issue.

simonewebdesign avatar Jun 17 '19 08:06 simonewebdesign