2020 Update
These options are now documented officially by node. For a 2GB machine, you should probably use:
NODE_OPTIONS=--max-old-space-size=1536
What are 'new space' and 'old space' anyway?
There's an excellent answer on "what are new space and old space?".
To determine the amount to use
You can see available memory on a Linux machine using free -m
. Note that you can consider the total of the free
and the buffers/cache
memory as available, as buffers/cache
can be thrown away instantly when needed (these buffers and cache are a nice way to use otherwise unused memory).
The official documentation formax-old-space-size
also mentions:
On a machine with 2GB of memory, consider setting this to 1536 (1.5GB)
Hence the value above. Consider that the amount of memory needed for the base OS doesn't change much, so you could happily do 3.5 on a 4GB machine etc.
To notice the defaults and see the effect of changes:
The default is 2GB:
$ node
> v8.getHeapStatistics()
{
....
heap_size_limit: 2197815296,
}
2197815296 is 2GB in bytes.
When set to 8GB, you can see heap_size_limit
changes:
$ NODE_OPTIONS=--max_old_space_size=8192 node
Welcome to Node.js v14.17.4.
Type ".help" for more information.
> v8.getHeapStatistics()
{
...
heap_size_limit: 8640266240,
...
}
As @Venryx mentions below you can also use process.memoryUsage()
--optimize-for-size
to reduce memory usage. – Downgrade