简体   繁体   English

Node JS pm2最大限制memory

[英]Node JS pm2 maximum memory limit

I'm using pm2 and node.js. What I want to do is set a maximum memory limit for an application.我正在使用 pm2 和 node.js。我想做的是为应用程序设置最大 memory 限制。 My dev server's limit is 500mb split between two applications.我的开发服务器的限制是在两个应用程序之间分配 500mb。 Ideally I would like to limit it to 200mb.理想情况下,我想将其限制为 200mb。 I need this limit to be a hard limit for the application, and for it to not restart the application if it exceeds it like the pm2 command "max_memory_restart" does.我需要这个限制作为应用程序的硬限制,并且如果超过它就不会像 pm2 命令“max_memory_restart”那样重新启动应用程序。 I've also tried to use the --max_old_space_size command with no luck.我也尝试过使用 --max_old_space_size 命令但没有成功。 Is there any way to do this?有什么办法吗?

Example pm2 json file示例 pm2 json 文件

{
  "apps": [
   {
     "name": "Sample",
     "script": "runit.js",
     "watch": false,
     "cwd": "xxxxxx",
     "env": {
       "NODE_TLS_REJECT_UNAUTHORIZED": '0',
       "NODE_ENV": "local_public",
       "PORT": 3000,
       "PROXY_PORT": 4000,
     },
     "args": [
       "--max_old_space_size=200"
     ]
   }
 ]

} }

I used pm2 to directly run node.js scripts and this code works for me:我使用 pm2 直接运行 node.js 脚本,这段代码对我有用:

pm2 start file.js --max-memory-restart 100M

Hope the max-memory-restart argument works for you希望 max-memory-restart 参数对你有用

You have to use node_args instead of args .您必须使用node_args而不是args Also, to be safe you can manually set max_memory_restart this option enables you to restart your process before it crashes.此外,为了安全起见,您可以手动设置max_memory_restart此选项使您能够在崩溃之前重新启动进程。

{
  "apps": [
   {
     "name": "Sample",
     "script": "runit.js",
     "watch": false,
     "cwd": "xxxxxx",
     "env": {
       "NODE_TLS_REJECT_UNAUTHORIZED": '0',
       "NODE_ENV": "local_public",
       "PORT": 3000,
       "PROXY_PORT": 4000,
     },
     "max_memory_restart": "180",
     "node_args": [
       "--max_old_space_size=200"
     ]
   }
 ]
}

You said you already tried the --max-old-space-size command with no luck.您说您已经尝试过 --max-old-space-size 命令但没有成功。

It looks like you tried to pass it via "args" pm2 command.看起来您试图通过“args”pm2 命令传递它。 This doesn't work for one main reason.由于一个主要原因,这不起作用。 It needs to be told explicitly to pass the command to the nodeJS process it is handling.它需要被明确告知将命令传递给它正在处理的 nodeJS 进程。

The --node-args= allows you to pass typical NodeJS commands while you are using PM2. --node-args= 允许您在使用 PM2 时传递典型的 NodeJS 命令。 Here are two ways of passing it.这里有两种传递它的方法。 First is via a direct package.json file script (leaving this here for others just in case):首先是通过直接的 package.json 文件脚本(将其留给其他人以防万一):

{
"start:prod": "pm2 start server.js --node-args='--max-old-space-size=200'"
}

or as the commenter above posted via the pm2 config file:或者正如上面的评论者通过 pm2 配置文件发布的那样:

{"node_args": ["--max-old-space-size=200"]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM