Published on

How to fix Nodejs heap out of memory error?

This problem occur when you run a program that consumers more heap memory than available.

Run the following command to see heap memory available for Nodejs:

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'

To increase this limit set --max-old-space-size environment variable to how much you want to increase

Increase heap memory size on macos and linux

export NODE_OPTIONS=--max-old-space-size=4096

Increase heap memory size on windows

Powershell

$env:NODE_OPTIONS="--max-old-space-size=4096"

Run the following command again you should see a higher value now, indicating heap memory has been increased.

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'

If you issue is not resolved even after setting limit to a higher value. You should check your application for any possible memory leaks.