Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Last active November 18, 2023 20:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjunghans/90ff3bbf575b8b1da41f3fb56e374931 to your computer and use it in GitHub Desktop.
Save tjunghans/90ff3bbf575b8b1da41f3fb56e374931 to your computer and use it in GitHub Desktop.
Node V8 max-old-space-size

Node V8 Option max-old-space-size

Command line

node --help --v8-options | grep -B 1 -A 1 max-old-space
        type: bool  default: false
  --max-old-space-size (max size of the old space (in Mbytes))
        type: size_t  default: 0

"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.

Setting

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

References

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