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

ng build fail with 'JavaScript heap out of memory' with patch 6.0.3

Open ArnaudBarre opened this issue 8 years ago • 55 comments

Versions

Angular CLI: 6.0.3
Node: 8.11.1
OS: darwin x64 (MacBook Pro 8Go RAM)
Angular: 6.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, upgrade
 
Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.3
@angular-devkit/build-angular     0.6.3
@angular-devkit/build-optimizer   0.6.3
@angular-devkit/core              0.6.3
@angular-devkit/schematics        0.6.3
@angular/cli                      6.0.3
@ngtools/webpack                  6.0.3
@schematics/angular               0.6.3
@schematics/update                0.6.3
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.8.3

Repro steps

ng build --prod

Observed behavior

92% chunk asset optimization UglifyJSPlugin [.. waiting for long time] FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Desired behavior

Build works

Mention any other details that might be useful (optional)

Back to 6.0.1 and 0.6.1 like few days ago works. For the moment I didn't change the default memory allocate to node.

ArnaudBarre avatar May 17 '18 09:05 ArnaudBarre

Maybe it's somehow related to update of rxjs and it's new importing rules? Maybe it adds more work to webpack?

th0r avatar May 23 '18 17:05 th0r

I wouldn't expect that, but maybe. I'll be doing some performance benchmarking soon and be sure to keep an eye out on the memory usage.

filipesilva avatar May 23 '18 17:05 filipesilva

@filipesilva I've found an issue...but I don't have words... That was because of updated lodash types... After reverting them back from 4.14.109 to 4.14.70 memory usage went down to ~1.4gb and there is no "out of memory" error anymore.

I can't believe it! I didn't even include it in the diff I posted above because I thought it doesn't matter at all! WTF?!

My only assumption is that they added a few recursive types like PartialDeep that consume a lot of memory but I'm not sure about anything in this world anymore.

th0r avatar May 23 '18 19:05 th0r

Well that's odd. But also interesting. Can you give me the lodash before and after versions? Maybe we can come up with a simple repro and pinpoint the problem. A lot of people might be suffering from this. Used lodash imports would also be useful.

filipesilva avatar May 24 '18 07:05 filipesilva

You can find before/after versions in the comment above. We import the whole lodash and use quite a lot of it's methods including _.chain.

th0r avatar May 24 '18 07:05 th0r

@filipesilva to summ up:

Node.js v10.1.0 with --max_old_space_size=8192 / webpack v4.8.3 / AoT build without minification:

  • @types/lodash v4.14.109 - 2.72gb / ~50 seconds
  • @types/lodash v4.14.70 - 1.62gb / ~50 seconds

Without max_old_space_size set:

  • @types/lodash v4.14.109 - out of memory error after ~20 minutes
  • @types/lodash v4.14.70 - 1.33gb / ~52 seconds

P.S. lodash v4.14.70 is not compatible with TS 2.7 out of the box - TS throws error node_modules/@types/lodash/index.d.ts(13154,53): error TS2344: Type 'T' does not satisfy the constraint 'object'. To fix it replace this line with isWeakSet<T extends object>(value?: any): value is WeakSet<T>;.

th0r avatar May 24 '18 10:05 th0r

I have the same problems like @ArnaudBarre . But even with the Update to node v10 our travis prod build with angular 6 fails https://travis-ci.org/utetrapp/geli/builds/395360885#L1108. If anybody has an Idea that would be great!

PatrickSkowronek avatar Jun 22 '18 08:06 PatrickSkowronek

@PatrickSkowronek try to increase the Node.js heap size using --max_old_space_size flag.

th0r avatar Jun 22 '18 08:06 th0r

@th0r yes I did, I gave it 4096 Mb which should be enough.

PatrickSkowronek avatar Jun 22 '18 08:06 PatrickSkowronek

@filipesilva After upgrade to Angular v6, my builds are always out of memory when execute ng build --prod. But after i remove @types/lodash, the build can be passed. So much thanks for @th0r . But the build time is still much slower than using Angular v5.(About double times). And I have already closed the build optimizer and using Node.js 10. My Angular version is 6.0.6. Cli version is 6.0.8. It seems the production build time is the main reason which blocking many users upgrade to new Angular version. About half year ago when i try to upgrade to Angular5, i met out of memory issue when i'm using the production build with AOT. I can't find a solution so i can only wait. I wait for about 2 month and update to the latest Angular cli version, the AOT build works.

BTW, currently if i build the production version in my local, it works. But when i try to build it in the CI(I'm using Appveyor), it will throw out of memory error until i remove @types/lodash as i mentioned above. But the CI build works well with Angular v5 and Cli version 1.7.x. For many scenarios of the deployment, the production build should be happened in the CI environment which means it may not have a very good hardware as your local PC. But it doesn't mean that the build should not be passed in the CI.

lokyoung avatar Jun 26 '18 04:06 lokyoung

@lokyoung @th0r How did you determine it was @types/lodash causing your issue? I don't have those installed, but am having same issue where can build successfully on local machine with memory limit set to 8192, but on appveyor it fails with out of memory exception.

PTC-JoshuaMatthews avatar Jul 09 '18 20:07 PTC-JoshuaMatthews

@PTC-JoshuaMatthews update part of dependencies, run build and check memory usage. If it's ok then update another part of dependencies and check memory usage etc.

th0r avatar Jul 09 '18 20:07 th0r

turn off the sourcemap and we get very good build performance. Angular version: 6.0.6 and 6.0.9

nnworkspace avatar Jul 19 '18 14:07 nnworkspace

Running NodeJs: 10.1.0 on aws CodeBuild @angular/cli: "6.0.8",

I can confirm that the issue is when using the sourcemap: true configuration flag that causes the problem. turned it off and everything works as expected.

stefan-karlsson avatar Jul 20 '18 13:07 stefan-karlsson

But what if you need source-maps in your prod build - what to do?

alexbjorlig avatar Jul 24 '18 13:07 alexbjorlig

@dauledk i would recommend to downgrade the cli to a version where build works. And wait for a fix in a future release.

stefan-karlsson avatar Jul 24 '18 18:07 stefan-karlsson

Hi @stefan-karlsson. Could you point what cli version is working for u. Just now, I have downgraded it to the 6.0.1 and I still have issue.

volodymyro-in6k avatar Jul 25 '18 20:07 volodymyro-in6k

My solution was to explicitly specify in the tsconfig.ts sourcemap: false for the expected environment. Even if it's supposed to be set at false by default.

ksavidetove avatar Jul 28 '18 02:07 ksavidetove

Hi all,

Are you still seeing this with the latest versions? Does anyone have some up to date reproduction instructions that I can follow to see it too?

filipesilva avatar Jan 29 '19 14:01 filipesilva

@filipesilva our project has grown a little and here are the latest results:

Angular CLI: 7.2.3
Node: 10.15.1
OS: darwin x64
Angular: 7.2.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router, upgrade

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.3
@angular-devkit/build-angular     0.12.3
@angular-devkit/build-optimizer   0.12.3
@angular-devkit/build-webpack     0.12.3
@angular-devkit/core              7.2.3
@angular-devkit/schematics        7.2.3
@angular/cdk                      7.3.0
@angular/cli                      7.2.3
@ngtools/webpack                  7.2.3
@schematics/angular               7.2.3
@schematics/update                0.12.3
rxjs                              6.4.0
typescript                        3.1.6
webpack                           4.29.0

Build (sourcemaps off, minimizer off):

  • max_old_space_size=8192: 1.45 mins / 3.57gb mem peak
  • no max_old_space_size: ~1 min / JavaScript heap out of memory error

So I can't see any progress here.

th0r avatar Jan 30 '19 12:01 th0r