Heroku dyno memory for Node.js


This post goes over how to optimize Heroku dyno memory for Node.js applications.

Problem

If your Node.js app is crashing with the error:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Or your Heroku memory usage looks like this:

Heroku memory usage metrics for a Node.js app

Then you need to check your app for memory leaks or upgrade to a larger dyno size.

max-old-space-size

If your dyno has more than 512 MB RAM, then you’re not making use of all the available memory (depending on your Node.js version, the default memory limit can be 512 MB).

To increase the Node.js memory limit, set --max-old-space-size in the CLI:

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

Or set it as an environment variable with key NODE_OPTIONS and value --max-old-space-size=1024:

NODE_OPTIONS='--max-old-space-size=1024'

1024 MB is equivalent to 1 GB so to set the size to 2 GB, double the number.

See more information on Heroku and Stackoverflow.

Cluster

Fork multiple clusters to take advantage of multiple cores to optimize Node.js concurrency.

Read more on optimizing dyno usage.

Metrics

Enable visibility into memory usage with Heroku Labs log-runtime-metrics.

Librato is an add-on for performance monitoring.



Please support this site and join our Discord!