user2280167
user2280167

Reputation: 95

Building Angular project lead to heap out of memory

When trying to build an Angular project on a Linux server with -aot flag, I get an error. I tried upgrading my instance to a higher CPU core, more RAM and turn on a SWAP partition but still the error just occur faster.

Here's while trying to build from terminal along with the command with the flags I use.

ng build --target=production --environment=prod --aot
Your global Angular CLI version (1.7.2) is greater than your local
version (1.4.7). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
 10% building modules 5/5 modules 0 activeTemplate parse warnings:                 
The <template> element is deprecated. Use <ng-template> instead ("
[WARNING ->]<template [ngIf]="!isClosed">
  <div [class]="'alert alert-' + type" role="alert" [ngClass]="classes""): ng:///****************node_modules/ngx-bootstrap/alert/alert.component.d.ts.AlertComponent.html@1:0
 92% chunk asset optimization                                                             
<--- Last few GCs --->

  133611 ms: Mark-sweep 1302.6 (1435.2) -> 1297.8 (1435.2) MB, 1112.6 / 0.0 ms [allocation failure] [GC in old space requested].
  134724 ms: Mark-sweep 1297.8 (1435.2) -> 1297.7 (1435.2) MB, 1112.7 / 0.0 ms [allocation failure] [GC in old space requested].
  135854 ms: Mark-sweep 1297.7 (1435.2) -> 1302.7 (1406.2) MB, 1129.8 / 0.0 ms [last resort gc].
  136983 ms: Mark-sweep 1302.7 (1406.2) -> 1307.9 (1406.2) MB, 1128.8 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0xb8bacacfb39 <JS Object>
    1: find_variable [0xb8baca04381 <undefined>:~3539] [pc=0xbdb4151ee32] (this=0xa9144042161 <an AST_Function with map 0xd6ccb950f99>,name=0x1f852792bb29 <String[12]: DOMException>)
    2: visit [0xb8baca04381 <undefined>:3449] [pc=0xbdb4151d969] (this=0x3da6d780b621 <a TreeWalker with map 0xd6ccb9394c9>,node=0x2f9919fb8e91 <an AST_SymbolRef with map 0xd6ccb9628c9>,descend=0x2e68ca83edd1 <JS Fu...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [ng]
 2: 0x109bf8c [ng]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [ng]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [ng]
 5: v8::internal::Factory::NewInternalizedStringImpl(v8::internal::Handle<v8::internal::String>, int, unsigned int) [ng]
 6: v8::internal::StringTable::LookupString(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>) [ng]
 7: v8::internal::LookupIterator::PropertyOrElement(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, bool*, v8::internal::LookupIterator::Configuration) [ng]
 8: v8::internal::Runtime::GetObjectProperty(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>) [ng]
 9: 0xed3501 [ng]
10: v8::internal::Runtime_KeyedGetProperty(int, v8::internal::Object**, v8::internal::Isolate*) [ng]
11: 0xbdb262092a7
Aborted (core dumped)

I tried the solution mentioned here, https://www.npmjs.com/package/increase-memory-limit But still with no luck

Upvotes: 6

Views: 25543

Answers (5)

Ramil Aliyev 007
Ramil Aliyev 007

Reputation: 5480

I faced same issue on Angular 4 project. The problem is my Node Js version is not compatible Angular 4, I installed Node Js v6.17.1 and problem resolved for me.

For more details Angular and Node Js compatibility you can visit this link : Is there a compatibility list for Angular / Angular-CLI and Node.js?

Upvotes: 0

RandomUser
RandomUser

Reputation: 1224

As Filipe Silva recommends in the angular-cli repository, add a new entry in the scripts section of your package.json file:

"ng-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng",

and use it this way:

npm run ng-high-memory -- 

You have to remember to use the double dash before the arguments, otherwise they might not be parsed correctly:

npm run ng-high-memory -- serve

Upvotes: 1

amritashan
amritashan

Reputation: 583

This does not look like a hardware limitation problem as such which can be resolved by upgrading the memory or processor. I once had a build running out of memory, but it turned out that there was a circular reference where a source file A required a source file B and then B also required A. It was a code design issue, of course.

I would suggest you to try a simple build first and check to see if it goes well.

ng build

Also, the Angular doc mentions that when using ng build --prod, (not --target=production), it performs an AOT compilation, among other things:

https://angular.io/guide/deployment

The --prod meta-flag engages the following optimization features.

Ahead-of-Time (AOT) Compilation: pre-compiles Angular component templates.

Production mode: deploys the production environment which enables production mode.

You might want to install the latest version of the Angular CLI and see if that helps.

Upvotes: 0

Shardul
Shardul

Reputation: 1004

For prod build, run following command:

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --target production

For more optimization flags, below command:

It has inbuilt cache busting and other goodies:

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --target production --build-optimizer --vendor-chunk

Upvotes: 6

Vaibhav
Vaibhav

Reputation: 1509

try running

node --max-old-space-size=8192

where 8192 is size in MB

Upvotes: 1

Related Questions