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

Please keep the discussion on topic and review the code of conduct for behavior.

https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md

gnomeontherun avatar Jul 31 '19 18:07 gnomeontherun

@maxisam

Thank you for the suggestion about Sass, but I am using plain CSS that is built in its own project. I didn't mention it earlier because the question of whether this was happening with plain CSS appears to be already answered.

Senneseph avatar Jul 31 '19 18:07 Senneseph

@Senneseph any chance I could see that project? I don't think a 24 component project should use 16gb of memory. I haven't seen any project that uses that much memory at all really

There was a severe, but rare, but with typescript 3.4 (https://github.com/microsoft/TypeScript/issues/30050) that caused a lot of extra memory to be used in some cases with union types. We've just released support for TS 3.5 in Angular 8.2.0 with @angular-devkit/[email protected]. That might be what's affecting your project, you can try updating to those versions to see if it helps.

filipesilva avatar Jul 31 '19 21:07 filipesilva

Thanks @filipesilva . Unfortunately, I can't grant access to the project itself. I will try the @angular-devkit/build-angular version you recommended and report back how it does in a week or so.

While I don't have many components, they are quite abstract. And while I'm not using the keyof operator directly, I do define a type that is a union of about 19 types. Perhaps somewhere I have combined this type with another in a bad way such that it explodes the tsc when it tries to perform normalization.

(if this is the reason that my angular app is normal on desktop browsers, but almost totally unresponsive when reactive forms are on the page, i will be really happy. For some reason, only Opera Mobile has the same responsiveness / cpu usage as desktop browsers.)

Senneseph avatar Jul 31 '19 22:07 Senneseph

Last week, after merging a relatively small branch into the master branch, the build of my project started to experience this memory issue reported here. The interesting part is that the problem was happening on the CI server only, none of the developers was having trouble locally, even with --prod.

After reading @filipesilva 's comment above, I decided to update angular as follows:

$ ng update @angular/cli @angular/core
Using package manager: 'npm'
Collecting installed dependencies...
Found 60 dependencies.
Fetching dependency metadata from registry...
    Updating package.json with dependency @angular/cli @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/core @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/animations @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/compiler-cli @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/language-service @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/router @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/forms @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/common @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/platform-browser-dynamic @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular-devkit/build-angular @ "0.802.1" (was "0.801.2")...
    Updating package.json with dependency @angular/compiler @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency @angular/platform-browser @ "8.2.1" (was "8.1.2")...
    Updating package.json with dependency typescript @ "3.5.3" (was "3.4.5")...
UPDATE package.json (2341 bytes)

The problem is now fixed!

My impression is that the extra memory needed for DL dual builds, plus the fact that typescript was bumped to 3.4 which has the aforementioned problems regarding union type edge cases, started causing this extra memory consumption for some projects.

bcmedeiros avatar Aug 12 '19 09:08 bcmedeiros

Hey all,

Just wanted to give you an update on the steps we (and others in the community) took to address this problem in Angular 8:

  • Added support for TypeScript 3.5 https://github.com/angular/angular/pull/31615
  • Improve performance when many lazy chunks are present https://github.com/webpack/webpack/pull/9472
  • Fix performance regression in Node 12 https://github.com/angular/angular/pull/31638
  • Warn when large component CSS is included https://github.com/angular/angular-cli/pull/15093
  • Warn when TS compilation includes unused files https://github.com/angular/angular-cli/pull/15061
  • Reduce amount of files processed by Build Optimizer https://github.com/angular/angular-cli/pull/14998
  • improved performance of Differential Loading builds https://github.com/angular/angular-cli/pull/15246
  • cleared up more resources on non-watch builds https://github.com/angular/angular-cli/pull/15292

These efforts are still under way:

  • Improve JS minifier performance https://github.com/webpack-contrib/terser-webpack-plugin/issues/104
  • Improve Sass processor performance https://github.com/sass/dart-sass/issues/744
  • Reduce memory used by the Angular Compiler
  • Remove pure_getter optimizations https://github.com/angular/angular-cli/issues/14316

Here's what you can do to take advantage of these fixes:

One special note is that we completely reworked how Differential Loading works. It should now be much faster, but can use more system memory depending on the numbers of cores you have. This is different from the node memory limit.

When you hit the node memory limit you see the FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory error. But if you hit the system memory limit you should see Fatal process OOM in insufficient memory to create an Isolate.

The new Differential Loading setup makes it rarer to hit the first, but if you have many cores but not a lot of memory, it can be more common to hit the second. We'd like feedback on @angular-devkit/[email protected] please, so that we can address this behaviour if it's a problem, by disabling parallelism in some cases or make it configurable.

filipesilva avatar Aug 15 '19 18:08 filipesilva

I have the same error in Angular 8 and this solved my problem:

node --max_old_space_size=X node_modules/@angular/cli/bin/ng build --prod

Where X = (2048 or 4096 or 8192 o..) is the value of memory

gokujy avatar Aug 19 '19 11:08 gokujy

@filipesilva just install Node 12.8 solved the issue . thanks.

yantrab avatar Aug 26 '19 06:08 yantrab

hello! very interesting thread especially this : https://github.com/angular/angular-cli/issues/13734#issuecomment-506760767 and this : https://github.com/angular/angular-cli/issues/13734#issuecomment-521743839

Can someone explain this to me :

Install node-sass as a dev dependency if you use Sass

is this us "giving up" on dart-sass?

or is it a combination of using dart-sass as main compiler and node-sass as "support", if that works at all? (the context of whether or not npm i sass --save-dev/yarn add sass --dev would still be in use in this case was not given in the above guide).

what exactly would my package.json file look like and are there any other changes to make to my project?

I am personally upgrading my project to angular 8 and want to switch to dart-sass

EDIT :

are there any other changes to make to my project?

to answer at least that part of my own question : it seems dart-sass does not support the /deep/ scss tag.

however ::ng-deep works so it's the best of all worlds. I'm really liking dart-sass so far! so much less of a hassle when building.

tatsujb avatar Sep 05 '19 09:09 tatsujb

I had this problem and this solution worked for me:

in the angular.json file, replace the value of the outputPath key with "dist"

Flavio-occhipinti avatar Sep 08 '19 09:09 Flavio-occhipinti

Just updating the node version from LTS to Current solved my issue.

akil3 avatar Sep 09 '19 20:09 akil3

same issue. node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod still doesn't work.

iamcco avatar Sep 10 '19 09:09 iamcco

Faced crashes from a large application with lots of chunks. But it seems a problem in source map and has same error how in https://github.com/webpack/webpack-sources/issues/66

Tom910 avatar Sep 10 '19 21:09 Tom910

I had to upgrade to node 12 AND increase our codebuild instance memory from 3 to 7GB. Just to build an angular app.

ktersius avatar Sep 12 '19 03:09 ktersius

@filipesilva, I confirm that ng build is working well now with:

  • angular 8.2.2
  • angular/cli 8.2.2
  • node 12.4

ng build --configuration=production

angular.json

					"configurations": {
						"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"
								}
							],
							"serviceWorker": true
						}

A few months ago when you tested my big fat SPA, the memory consumption of node was over 2GB, so I had to use: node --max_old_space_size=8192 "node_modules/@angular/cli/bin/ng" build --configuration=production

Now with a larger codebase being built with ng build, the memory peak of node is 1.6GB.

Good work. :)

zijianhuang avatar Sep 12 '19 04:09 zijianhuang

Is there any other way to make this work on node 10?

stevefiedler avatar Sep 12 '19 19:09 stevefiedler

@stevefiedler , is there any reason why you have to stay on node 10?

zijianhuang avatar Sep 12 '19 21:09 zijianhuang

Well, jumping to 12 seemed to require a lot more changes for my application, and I have multiple applications to worry about. It should be working a step at a time

stevefiedler avatar Sep 13 '19 12:09 stevefiedler

@stevefiedler , I have a big fat Angular SPA started since Sep 2016 with Angular 2, and it seem I did not encounter any problem when moving to node 12. If you post the problem to StackOverflow, you may find more helps and I may have a look as well.

zijianhuang avatar Sep 13 '19 13:09 zijianhuang

@stevefiedler Out of pure curiosity. What problems do you have with your application when you change the compiler to Node 12?

manklu avatar Sep 13 '19 22:09 manklu