How to limit CPU and memory usage for node processes
Asked Answered
A

4

23

I would like to install a Ghost Blog on a shared server via GitHub. During the installation, I need to run npm install, grunt init and grunt prod. My host provides 500 MB memory usage, if a process uses more than 600 MB it kills it.

Therefore I need an option to limit the memory usage of these processes because they all need more than 500 MB of memory!

I tried to run the processes with --max-old-space-size=450 but it does not seem to work.

I would be glad if anyone could provide me a link to a tutorial or documentation about running node processes with options.

Thank you!

UPDATE: Since I’ve posted this the installation of Ghost has changed completely.

Anachronistic answered 15/9, 2015 at 8:38 Comment(4)
If your host OS is linux you can use cgroups to limit memory usage of ANY process.Treatment
cgroups isn’t in stalled, and i am not allowed to install it. But thank you!Anachronistic
slebetman could you give an example of how to use cgroups to limit memory usage of a process?Renfrew
So nothing for CPU?Kano
R
22

From node v8+, typing the following:

node --help

show a --v8-options option. Then typing:

node --v8-options

gives:

...
--max_old_space_size (max size of the old space (in Mbytes))
    type: int  default: 0
--initial_old_space_size (initial old space size (in Mbytes))
    type: int  default: 0
...

I have managed to use the first option like this:

node --max-old-space-size=250 `which npm` install

Here we told node to limit RAM usage to 250Mo, the 'which npm' part gives the current npm path and 'install' is the script you want to run.

Romish answered 22/1, 2018 at 22:7 Comment(2)
Probably I don’t need is anymore, but out of curiosity let’s say I have a typical npm project with a package.json and I want to limit the CPU usage of the start script, I can navigate to the app directory and type node --max-old-space-size=250 npm start.Anachronistic
@Anachronistic Absolutly ! I currently have an automated deployment pipeline on a project that execute this command to avoid exceeding the maximum RAM usage amount allowed by a limited Docker container.Romish
P
6

I use the following and it works like a charm

NODE_OPTIONS=--max_old_space_size=50 npm install
Pericope answered 7/5, 2021 at 14:5 Comment(0)
C
4

This is how you do it.

You send a command to limit the ram usage.

npm install --max-old-space-size=400
Coveney answered 13/12, 2016 at 10:18 Comment(3)
Thank you for your answer. But as I described in my original post, I’ve tried that and it did not seem to work. But anyway I found a work around.Anachronistic
@tomexx I just didn’t. I worked around it. The project I wanted to build on the server I built it locally.Anachronistic
@Anachronistic Ahh :( Thats a pity.Hyperdulia
W
4

To limit the CPU usage to 80%:

cpulimit -l 80 npm install

install

sudo apt-get install cpulimit # Debian based
# OR
sudo yum install cpulimit.    # REHL based
Warrenwarrener answered 21/8, 2023 at 21:58 Comment(1)
great to meet cpulimit command. Available on brew as well: brew install cpulimit. Unfortunately it doesn't work for a node.js process even using the -i option.Folsom

© 2022 - 2024 — McMap. All rights reserved.