Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in Ionic 3

When I run an Ionic 3 project using the ionic serve command, then I am getting this error:

Screenshot of FATAL ERROR: ineffective mark-compacts near heap limit Allocation failed - JavaScript

like image 685
Himanshu Shekhar Avatar asked Nov 09 '18 17:11

Himanshu Shekhar


People also ask

How do I fix reached heap limit allocation failed JavaScript heap out of memory?

To fix JavaScript heap out of memory error, you need to add the --max-old-space-size option when running your npm command. Alternatively, you can also set the memory limit for your entire environment using a configuration file.

What is JavaScript heap out of memory?

js project is the “JavaScript heap out of memory” error. This error usually occurs when the default memory allocated by your system to Node. js is not enough to run a large project. The error is common whether you run your project on Windows, macOS, or a Linux distribution like Ubuntu.


4 Answers

For a non-Angular general answer for those who land on this question from Google:

Every time you face this error it’s probably because of a memory leak or difference between how Node.js <= 10 and Node.js > 10 manage memory.

Usually just increasing the memory allocated to Node.js will allow your program to run but may not actually solve the real problem and the memory used by the node process could still exceed the new memory you allocate. I'd advise profiling memory usage in your Node.js process when it starts running or updating to Node.js > 10.

I had a memory leak. Here is a great article on debugging memory leaks in Node.js.

That said, to increase the memory, in the terminal where you run your Node.js process:

export NODE_OPTIONS="--max-old-space-size=8192"

where values of max-old-space-size can be: [2048, 4096, 8192, 16384] etc

More examples for further clarity:

export NODE_OPTIONS="--max-old-space-size=5120" # Increase to 5 GB
export NODE_OPTIONS="--max-old-space-size=6144" # Increase to 6 GB
export NODE_OPTIONS="--max-old-space-size=7168" # Increase to 7 GB
export NODE_OPTIONS="--max-old-space-size=8192" # Increase to 8 GB

# and so on...

# formula:
export NODE_OPTIONS="--max-old-space-size=(X * 1024)" # Increase to X GB

# Note: it doesn't have to be multiples of 1024.
# max-old-space-size can be any number of memory megabytes (MB) you have available.

See the current value of max-old-space-size (in MB)

To see the current (not exact but very close) value of max-old-space-size (in MB), run in your terminal

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'
like image 91
Emmanuel N K Avatar answered Oct 16 '22 14:10

Emmanuel N K


In my case, I fixed this problem by installing Node.js, version 12.10.0.

like image 94
Germán Ayala Avatar answered Oct 16 '22 15:10

Germán Ayala


I had the same issue on CentOS server 7, but 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.

like image 82
Hakim CHRIFI ALAOUI Avatar answered Oct 16 '22 16:10

Hakim CHRIFI ALAOUI


Just type this in the terminal:

export NODE_OPTIONS="--max-old-space-size=8192"

The error occurs when you exceed the default maximum memory allowed for Node.js. All this does is increase the maximum memory allowed.

like image 67
Matt Avatar answered Oct 16 '22 16:10

Matt