Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit Node.js memory usage to less than 300MB per process

We run tests in separate processes and a few of our test suites contain more than 20 files. How can we limit (instead of increase) the amount of memory a Node.js process uses to less than say 300MB? If we don't limit memory, we could use up to 20x500MB, which is about 10GB, which is too much memory.

I can find a few articles online about increasing memory, but I can't find anything on limiting memory per process.

Specifically, we are using the core child_process module to create child processes and I guess we need some flag or arg to pass to the cp's to designate a memory cap of sorts.

like image 763
Alexander Mills Avatar asked Dec 04 '15 06:12

Alexander Mills


People also ask

How do I limit memory in Node js?

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.

How do I increase my Node memory limit?

If you want to increase the max memory for Node you can use --max_old_space_size option. You should set this with NODE_OPTIONS environment variable.

How do I check Nodejs memory usage?

memoryUsage() method is an inbuilt method of the process module that provides information about the current processes or runtime of a Node. js program. The memory usage method returns an object describing the memory usage in bytes of the Node.


1 Answers

Useful links:

nodejs decrease v8 garbage collector memory usage

https://groups.google.com/forum/#!topic/nodejs/IYsQ_bXlzcg

https://www.quora.com/How-does-Node-js-do-memory-allocation

Options to be considered:

–nouse-idle-notification

–expose-gc + gc() function from your code.

–max-old-space-size

–max-new-space-size

--max-semi-space-size

Also to decrease memory, less memory consuming algorithms can be used, the code can be more GC friendly. Also native C++ modules can help, but check them thoroughly for memory leaks and errors with some valgrind.

Also quotes from OS can be applied to kill processes which exceed some memory limits.

I know, without some concious option descriptions this answer is not so usefull, but I not very good learned them for now, so this "answer" is just a direction.

like image 149
Dzenly Avatar answered Oct 16 '22 04:10

Dzenly