Skip to content

Instantly share code, notes, and snippets.

@stormwild
Last active June 29, 2024 12:03
Show Gist options
  • Save stormwild/4bd3c1ec50ed055a363012a403b16365 to your computer and use it in GitHub Desktop.
Save stormwild/4bd3c1ec50ed055a363012a403b16365 to your computer and use it in GitHub Desktop.
Node Optimization

Node

Optimization Flags

How do I determine the correct "max-old-space-size" for node.js?

The configured max-old-space-size had a higher limit than the total amount of RAM that the machine had. – Borjante

Worth trying --optimize-for-size to reduce memory usage. – Constantinos

"Old space" is the biggest and most configurable section of V8's managed (aka garbage-collected) heap (i.e. where the JavaScript objects live), and the --max-old-space-size flag controls its maximum size. As memory consumption approaches the limit, V8 will spend more time on garbage collection in an effort to free unused memory.

If heap memory consumption (i.e. live objects that the GC cannot free) exceeds the limit, V8 will crash your process (for lack of alternative), so you don't want to set it too low. Of course, if you set it too high, then the additional heap usage that V8 will allow might cause your overall system to run out of memory (and either swap or kill random processes, for lack of alternative).

In summary, on a machine with 2GB of memory I would probably set --max-old-space-size to about 1.5GB to leave some memory for other uses and avoid swapping.

The Node.js Runtime v8 options list

Node.js optimization flags lesson from production

If you use any of these flags in production, I want to encourage you to experiment with them and find what is optimal for your application. In my company services, we found --optimize_for_size to generally be a good thing to include, as otherwise RAM usage might sometimes go out of hand and causing long breaks for GC.

But for one service, it was actually harmful. This service typically have low RAM footprint, or at least, a footprint that the default settings can manage well enough.

10 Habits of a Happy Node Hacker (2016)

  1. Avoid garbage

    Node (V8) uses a lazy and greedy garbage collector. With its default limit of about 1.5 GB, it sometimes waits until it absolutely has to before reclaiming unused memory. If your memory usage is increasing, it might not be a leak - but rather node's usual lazy behavior.

    To gain more control over your app's garbage collector, you can provide flags to V8 in your Procfile:

    web: node --optimize_for_size --max_old_space_size=920 --gc_interval=100 server.js

    This is especially important if your app is running in an environment with less than 1.5 GB of available memory. For example, if you'd like to tailor node to a 512 MB container, try:

    web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 server.js

How to solve process out of memory in Node.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment