The fatal error: ineffective mark-compacts near heap limit allocation failed – javascript heap out of memory occurs if there is any memory leak or the application consumes a lot of memory.
In this article, we will look at what exactly is the fatal error: ineffective mark-compacts near heap limit allocation failed – javascript heap out of memory means and how to resolve this error.
What is fatal error: ineffective mark-compacts near heap limit allocation failed – javascript heap out of memory?
If you are building and running the application on Node.JS version 10, and if you have any kind of memory leak in the code, it can lead to javascript heap out of memory.
It can also happen if the application consumes too much memory and mainly while processing extensive data.
The memory management in Node V10 is entirely different when compared to the latest version, and by default, 512 MB of memory/heap size is allocated in Node 10. If the application crosses this size, it will result in a javascript heap out of memory.
How to fix fatal error: ineffective mark-compacts near heap limit allocation failed – javascript heap out of memory?
We can resolve this issue by installing the latest Node version or by increasing the default allocated memory size. Also, you need to check if there is any memory leak in the application.
Solution 1 – Install Latest Node Version
The Node 10 has a different way of memory allocation, and by default, the max size set is 512MB. Hence you can install the latest LTS Node 12/16 to resolve the issue.
Solution 2 – Increase the Memory Size using export
Increasing the default memory size will fix the issue; you could increase the memory allocation by running the below command.
The size can be of any number according to the needs, the below command is just an indicative examples on how to increase the memory size.
export NODE_OPTIONS="--max-old-space-size=5120" # Increases to 5 GB
export NODE_OPTIONS="--max-old-space-size=6144" # Increases to 6 GB
export NODE_OPTIONS="--max-old-space-size=7168" # Increases to 7 GB
export NODE_OPTIONS="--max-old-space-size=8192" # Increases to 8 GB
Solution 3 – Set the memory through NODE_OPTIONS
We can change the default memory size using the set NODE_OPTIONS
as shown below.
set NODE_OPTIONS=--max_old_space_size=4096
Note: All the solution above works effectively, but you should always ensure there is no memory leak on your application first before changing these values and also ensure you have the free memory space left.
Conclusion
The fatal error: ineffective mark-compacts near heap limit allocation failed – javascript heap out of memory happens when you run the application with Node 10 version, and your application consumes more than 512MB memory.
We can resolve the issue by upgrading to the latest version of Node as the memory allocation is managed efficiently. Alternatively, we can increase the default memory size using the NODE_OPTIONS
.