I did a bit of research on this topic and have few suggestions for you that might help.
What does the issue mean and what is the cause?
One of the scenarios when this happens is when the application batch processes a large set of data, and the data processing algorithm is written in a way such that it needs to hold onto the objects in heap space until the processing is done.
What can be done about this issue?
Never declare variables with keyword "Var" unless necessary (it has a gobal scope and occupies huge amount of memory), rather use "let", "const"
Good Practice: Always initialize arrays as an empty array
Good Practice: Always use functions to do any operation, in that way the variables that only require local scope will go to the garbage collector immediately after you exit from the function.
As a result, the freed memory can be allocated to other variables.
There is nothing unsafe about them, just that they do not run inside a VM
In Node.js, the maximum heap size is not set, a default memory limit will be imposed, and this default value varies based on the Node.js version and architecture of the system the program is running in. Therefore, It is recommended to always explicitly set the heap size instead of relying on default imposed by Node.js. One more point, this maximum size can also be increased.
The maximum heap size can be set/increased in the following manner:
node --max-old-space-size=4096 index.js #increase to 4GB