Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 466
user2280167 Avatar asked Jan 03 '23 03:01

user2280167


2 Answers

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

like image 96
Shardul Avatar answered Jan 05 '23 16:01

Shardul


try running

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

where 8192 is size in MB

like image 30
Vaibhav Kumar Goyal Avatar answered Jan 05 '23 15:01

Vaibhav Kumar Goyal