Increasing Node’s Memory

How to break out of the 1.76GB deafult memory limit for node.js processes.

Photo by Fredy Jacob on Unsplash

Yesterday our frontend build started running out of memory. Like a lot of people, we use vue's vue-cli to get our frontend from (what seems like) a million bespoke files into a universal set of javascript chunks. And for the most part that’s been fine. Until yesterday, when we started seeing:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

The best thing you can say about Javascript is that it’s probably the closest we’ve got to a universal programming language. However, some might argue that it was not designed to deal with heavy cpu/memory applications like the compilation and build steps modern frameworks put it through (think babel, webpack, uglify, etc.).

This error is caused by node.js running out of memory. Given what Javascript was originally designed for, node (or more specifically v8) makes the assumption that a single process isn’t going to need more than ~2GB of memory. And for most cases this is fine. However, it turns out that for our build steps, it wasn’t.

Luckily node let’s you up the memory limit with the archaically named --max-old-space-size=SPACE_IN_MB flag. And to make life even easier, it allows you to globally set these options in the environment, so you don’t need to worry about catching every node invocation yourself: export NODE_OPTIONS=--max-old-space-size=8192 (for example).

The lesson: If you need more than 1.76GB of memory in node, use the --max-old-space-size=SPACE_IN_MB argument (or NODE_OPTIONS environment variable) to get some more!