20

I am trying to limit a node.js application from using to much memory and I've found out about the --max-stack-size & --max_executable_size options that are available within V8 to pass when invoking the file, but how should I combine these and possible other arguments to limit max memory usage to 1GB?

Industrial
  • 41,400
  • 69
  • 194
  • 289

3 Answers3

16

https://github.com/joyent/node/wiki/FAQ

What is the memory limit on a node process?

Currently, by default v8 has a memory limit of 512mb on 32-bit systems, and 1gb on 64-bit systems. The limit can be raised by setting --max_old_space_size to a maximum of ~1gb (32-bit) and ~1.7gb (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.

Value is in megabytes, I believe.

James
  • 24,676
  • 13
  • 84
  • 130
Josh
  • 12,602
  • 2
  • 41
  • 47
  • 5
    The default limit increased to ~1.5GB around node 2. See https://github.com/nodejs/node/issues/3370#issuecomment-148108323 – hurrymaplelad Nov 12 '15 at 07:04
16

It's now, --max-old-space-size no technology limits...

for example node --max-old-space-size=8192 ./app. We create limit in 8Gb

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
1

There are two ways to increase the maximum memory size of V8:

  1. Using a CLI option: node --max-old-space-size=4096 memory.js
  2. Using an env variable: NODE_OPTIONS='--max-old-space-size=4096' node memory.js

Here's a small script that shows you the memory usage of your application:

const process = require('node:process');

const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`This app is currently using ${Math.floor(used)} MB of memory.`);
Benny Code
  • 51,456
  • 28
  • 233
  • 198