Node JS pm2 maximum memory limit
Asked Answered
F

3

13

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

}

Fug answered 22/3, 2017 at 22:19 Comment(2)
Your code will work if you just replace args with node_args.Curry
grizzlybit.info/blog/increase-nodejs-memory-limitColchester
E
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

Equalitarian answered 22/3, 2017 at 23:54 Comment(5)
Anyway to do this without restarting when it hits the max memory though...just keep it under a certain level?Fug
@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.Equalitarian
@Pytth Agree! Some people don't comment even when you ask why you get down vote.Equalitarian
@zh What is the default limit of max_memory_restart. If I don't add it, will it not restart at all?Chortle
@Burki 1300MB is default limitAargau
D
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"
     ]
   }
 ]
}
Decretory answered 10/4, 2020 at 8:38 Comment(1)
max_memory_restart number should have M at the end: "max_memory_restart": "180M"Basilius
R
1

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"]}
Raker answered 1/2, 2023 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.