Best ways to fix outofmemory issue in nodejs Applications in heap and process

This article, Discussed how to fix out-of-memory errors in Nodejs You can also check Fix for digital envelope routines::unsupported

Javascript or nodejs apps throw the below error stack trace.

Sometimes, the Running Nodejs application throws the below errors, making the application crash

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed — process out of memory

And another error

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

How do you resolve a fatal error ineffective Mark compacts near heap limit allocation failed JavaScript heap out of memory?

You can also check other posts on npm command deprecate option is deprecated

  • What is Out of memory issue?

An application needs some memory to run javascript programs. It stores application related objects in memory. It has default memory allocated

This error throws when an application is running with less allocated memory.

Fix for needs more memory than the allocated memory.

  • Where does this error occur?

This error will be shown in any javascript application using nodejs and javascript applications like Angular, Vue, and React EmberJS.

when you run one or more below commands

  • ng serve
  • npm start
  • npm install
  • npm run build
  • node application.js

The application gets slowed or crashed because of performance memory issues.

Memory allocated in Heap is 512MB by default.

CALL_AND_RETRY_LAST Allocation failed error is thrown when an application is allocated with a heap size more than the available default-free memory in a running environment.

Solution or Fix for CALL_AND_RETRY_LAST Allocation failed

There are multiple ways to fix it.

In NodeJS, there is a command-line flag —max-old-space-size and increase to memoryless than your physical server RAM.

suppose, You have 4GB RAM, change —max-old-space-size=Upto 4GB RAM.

node --max-old-space-size=4096 app.js

It starts the node process server with allocated memory of 4094 memory and applies to a single session.

Another way is to add the environment variable NODE_OPTION with this flag value

export NODE_OPTIONS=--max_old_space_size=4096

It applies to all sessions with this value.

You can also add a .bashrc file in Linux or Unix machine during the loading user profile startup.

export NODE_OPTIONS=--max_old_space_size=4096

Third, an option is to add npm scripts in the package.json file

"scripts": {
"start": "node --max_old_space_size=4096 index.js",
}

What is the maximum memory limit for Node?

Node settings contains --max_old_space_size property that needs memory limit for holding object for memory running node process. For example, If your machine is 4GB Memory, You can set 75% of memory allocated to this property

export NODE_OPTIONS=--max_old_space_size=3072

NodejS default memory settings

default memory limit allocated to node process based on nodejs version. For node 12 version or less, It allocates 1.5GB of an memory. Allocates 2GB memory for Node12 version or more. You can still change the default settings NODE_OPTIONS with --max_old_space_size

Conclusion

To Sump Up, heap memory error is due to insufficient allocated memory for running node process. Learned multiple ways to increase it.