最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS pm2 maximum memory limit - Stack Overflow

programmeradmin1浏览0评论

I'm using pm2 and node js. What I want to do is set a maximum memory limit for an application. My dev server's limit is 500mb split between two applications. Ideally I would like to limit it to 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. I've also tried to use the --max_old_space_size command with no luck. Is there any way to do this?

Example pm2 json file

{
  "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'm using pm2 and node js. What I want to do is set a maximum memory limit for an application. My dev server's limit is 500mb split between two applications. Ideally I would like to limit it to 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. I've also tried to use the --max_old_space_size command with no luck. Is there any way to do this?

Example pm2 json file

{
  "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"
     ]
   }
 ]

}

Share Improve this question asked Mar 22, 2017 at 22:19 N8ALL3NN8ALL3N 3851 gold badge2 silver badges14 bronze badges 2
  • 4 Your code will work if you just replace args with node_args. – Xaqron Commented Nov 4, 2017 at 16:51
  • grizzlybit.info/blog/increase-nodejs-memory-limit – zubair1024 Commented Oct 21, 2020 at 10:47
Add a comment  | 

3 Answers 3

Reset to default 26

I used pm2 to directly run node.js scripts and this code works for me:

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

Hope the max-memory-restart argument works for you

You have to use node_args instead of args. Also, to be safe you can manually set max_memory_restart this option enables you to restart your process before it crashes.

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

It looks like you tried to pass it via "args" pm2 command. 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.

The --node-args= allows you to pass typical NodeJS commands while you are using PM2. Here are two ways of passing it. First is via a direct package.json file script (leaving this here for others just in case):

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

or as the commenter above posted via the pm2 config file:

{"node_args": ["--max-old-space-size=200"]}
发布评论

评论列表(0)

  1. 暂无评论