{

Different ways to fix out of memory issues in nodejs heap and process


This article, Discussed how to fix out of memory errors in Nodejs Solution of outofmemory issues in Nodejs application

Javascript or nodejs apps throws the below error stack trace.

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

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
  • What is Out of memory issue?

An application needs some memory to run javascript programs.

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. ``markup 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.


Other way is to add environment variable NODE_OPTION  with this flag value
```markup
export NODE_OPTIONS=--max_old_space_size=4096

It applies to all sessions with this value.

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

export NODE_OPTIONS=--max_old_space_size=4096

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

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

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.