Search code examples
javascriptnode.jspm2

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

}


Solution

  • 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