如何限制节点进程的CPU和内存使用

问题描述 投票:0回答:4

我想通过 GitHub 在共享服务器上安装 Ghost 博客。在安装过程中,我需要运行

npm install, grunt init
grunt prod
。我的主机提供 500 MB 内存使用量,如果进程使用超过 600 MB,则会杀死它。

因此我需要一个选项来限制这些进程的内存使用,因为它们都需要超过 500 MB 的内存!

我尝试使用

--max-old-space-size=450
运行进程,但它似乎不起作用。

如果有人能为我提供有关使用选项运行节点进程的教程或文档的链接,我将很高兴。

谢谢!

更新: 自从我发布这篇文章以来,Ghost 的安装已经完全改变了。

node.js npm gruntjs ghost-blog
4个回答
20
投票

从节点 v8+ 中,输入以下内容:

node --help

显示 --v8-options 选项。然后输入:

node --v8-options

给出:

...
--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
...

我已经成功地使用第一个选项,如下所示:

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

这里我们告诉节点将 RAM 使用限制为 250Mo,“which npm”部分给出当前的 npm 路径,“install”是您要运行的脚本。


5
投票

我使用以下方法,它的效果就像一个魅力

NODE_OPTIONS=--max_old_space_size=50 npm install

3
投票

这就是你要做的。

您发送命令来限制内存使用。

npm install --max-old-space-size=400

0
投票

将 CPU 使用率限制为 80%:

cpulimit -l 80 npm install

安装

sudo apt-get install cpulimit # Debian based
# OR
sudo yum install cpulimit.    # REHL based
© www.soinside.com 2019 - 2024. All rights reserved.