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


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))'

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


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.