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

How to limit CPU and memory usage for node processes

我愿意install a Ghost Blog on a shared server via GitHub。在安装过程中,我需要 运行 npm install, grunt initgrunt prod。我的主机提供 500 MB 的内存使用量,如果一个进程使用超过 600 MB,它就会杀死它。

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

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

如果有人可以向我提供 link 关于 运行 带有选项的节点进程的教程或文档,我将很高兴。

谢谢!

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

这就是你的做法。

您发送命令限制 ram 使用。

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

从节点 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' 是你想要 运行 的脚本。

我用了下面的,效果很好

NODE_OPTIONS=--max_old_space_size=50 npm install