12

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

}

N8ALL3N
  • 375
  • 1
  • 2
  • 14

3 Answers3

25

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

zhangjinzhou
  • 2,461
  • 5
  • 21
  • 46
  • Anyway to do this without restarting when it hits the max memory though...just keep it under a certain level? – N8ALL3N Mar 23 '17 at 02:26
  • @MasterN8 At least I don't know anyway to do so. You won't be able t access the server when it's stopping and restarting. A way to reduce the frequency of reboot is to improve your code to reduce memory leak and also set the memory limit reasonable. – zhangjinzhou Mar 23 '17 at 15:41
  • 3
    @Pytth Agree! Some people don't comment even when you ask why you get down vote. – zhangjinzhou Mar 23 '17 at 15:43
  • 1
    @zh What is the default limit of `max_memory_restart`. If I don't add it, will it **not** restart at all? – Haseeb Burki Jun 17 '19 at 08:02
  • 1
    @Burki 1300MB is default limit – Vardan Jul 06 '19 at 10:48
10

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"
     ]
   }
 ]
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Amulya Kashyap
  • 2,333
  • 17
  • 25
0

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"]}
vostersc
  • 61
  • 1
  • 4