Skip to content

Automatically restart processes using more than a fixed amount of RAM #141

Description

@Ecco

It would be very valuable to get the option to kill and respawn processes that are using more memory than a configurable amount. This would help a lot in the case of a leaking app. Indeed, consider the following scenario:

  • No respawn: The leaking app ends up taking tons of memory, effectively bringing the server down
  • Auto respawn: It might indeed require a bit of CPU time, but the service can be kept up and running

I'm not saying that leaking apps are a good thing. But from a "host" point of view, killing and respawning seems like the best thing to do. This is what Heroku does, for example.

Activity

  1. rodrigok commented on Nov 4, 2013

    @rodrigok

    I need it too. Maybe restart based on amount of CPU usage too.

  2. abelaska commented on Nov 18, 2013

    @abelaska

    +1

  3. dandv commented on Mar 7, 2014

    @dandv
    Contributor

    👍 Very useful feature for production while debugging the leak in dev.

  4. tjwebb commented on Mar 27, 2014

    @tjwebb

    +1

  5. devsmart commented on Jul 5, 2014

    @devsmart

    +1 , since PM2 cluster_mode takes much memory. if it can be restart when memory reached 500MB like will be great. it takes around 1.5GB when we keep process 3,4 days :(

    image

  6. rlidwka commented on Jul 16, 2014

    @rlidwka
    Contributor

    I think you could set a memory limit in node.js/v8 command-line arguments, so it'll crash when the limit is reached. And pm2 will restart it as usual. Would this work for you?

  7. devsmart commented on Jul 17, 2014

    @devsmart

    Thanks @rlidwka you mean by V8_MAX_SEMISPACE_SIZE ? or any other way to set it on with PM2 ?
    I usually use simple PM2 command "pm2 start app.js -i max" appreciate if you can explain more how I can set memory limit to my application.

  8. rlidwka commented on Jul 17, 2014

    @rlidwka
    Contributor

    I'm talking about node --max-old-space-size=XXX test.js, with which a process that eats too much will die with "FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory".

    If you're using pm2, I suppose pm2 start --node-args="--max-old-space-size=XXX" test.js will give the same effect.

    I'm not sure if it's the right way, but it seems worth exploring.

  9. 4 remaining items

  10. Unitech commented on Aug 14, 2014

    @Unitech
    Owner

    I've added the option --max-restart-memory: https://github.com/Unitech/pm2/tree/development#automatic-restart-process-based-on-memory

    Thanks for that v8 trick @rlidwka !

  11. soyuka commented on Apr 13, 2015

    @soyuka
    Contributor

    @yorkie please see #1159.
    Could you show your pm2.log? This might be an issue in your app.

  12. ablankenship10 commented on Apr 13, 2015

    @ablankenship10

    I have the same problem where after about 8-10 hours, the pm2 instance runs of out of memory and never recovers. When I log into the server and run pm2 list, its like pm2 was never running and it restarts the Daemonizer. Here's my log. This is running on a 512MB Debian digital ocean droplet. I run it with the command 'pm2 start -x index.js'

    2015-04-10 21:02:42: Starting execution sequence in -fork mode- for app name:index id:0
    2015-04-10 21:02:42: App name:index id:0 online
    2015-04-11 06:50:00: ERROR CAUGHT BY DOMAIN:
    Error: spawn ENOMEM
        at exports._errnoException (util.js:746:11)
        at ChildProcess.spawn (child_process.js:1155:11)
        at exports.spawn (child_process.js:988:9)
        at Object.exports.execFile (child_process.js:682:15)
        at exports.exec (child_process.js:642:18)
        at getUrl (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/vizion/lib/git.js:15:5)
        at fn (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/async/lib/async.js:641:34)
        at Immediate._onImmediate (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/async/lib/async.js:557:34)
        at processImmediate [as _immediateCallback] (timers.js:358:17)
    child_process.js:1155
        throw errnoException(err, 'spawn');
              ^
    Error: spawn ENOMEM
        at exports._errnoException (util.js:746:11)
        at ChildProcess.spawn (child_process.js:1155:11)
        at exports.spawn (child_process.js:988:9)
        at Object.exports.execFile (child_process.js:682:15)
        at exports.exec (child_process.js:642:18)
        at getUrl (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/vizion/lib/git.js:15:5)
        at fn (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/async/lib/async.js:641:34)
        at Immediate._onImmediate (/root/.nvm/versions/node/v0.12.0/lib/node_modules/pm2/node_modules/async/lib/async.js:557:34)
        at processImmediate [as _immediateCallback] (timers.js:358:17)
    2015-04-13 15:54:37: [PM2][WORKER] Started with refreshing interval: 30000
    2015-04-13 15:54:37: [[[[ PM2/God daemon launched ]]]]
    2015-04-13 15:54:37: BUS system [READY] on port /root/.pm2/pub.sock
    2015-04-13 15:54:37: RPC interface [READY] on port /root/.pm2/rpc.sock
    2015-04-13 15:58:24: Starting execution sequence in -fork mode- for app name:index id:0
    2015-04-13 15:58:24: App name:index id:0 online
    
  13. jshkurti commented on Apr 13, 2015

    @jshkurti
    Contributor

    Maybe it is your app which takes large amounts of memory and thus PM2 doesn't have enough.

  14. ablankenship10 commented on Apr 13, 2015

    @ablankenship10

    Sure but shouldn't pm2 be catching that error and restart itself? Is it because I'm using fork mode? I guess I don't have to since it is only 1 core.. Its using jsdom and jquery and reading web page, I make sure to close the session each time but I'm sure its possible the dom library has some leaks in it.

    Idk if its a PM2 issue or a Node issue in general, but every app I've made I've noticed that memory use always constantly increases, its never dumped. Is there a fix for this that I've missed?

  15. jshkurti commented on Apr 13, 2015

    @jshkurti
    Contributor

    Well, the thing is, in order to update itself, PM2 calls spawn('pm2 update') but since there's not enough memory for spawn(), it doesn't work. That's why you see the error twice. The first time it gets triggered by a spawn() in the worker, the second time it gets triggered when pm2 tries to update itself.
    I think 512MB of RAM is just not enough nowadays to run your apps + a process manager while both of them use Node.js (which means being run by a VM such as Google V8 Runtime therefore using a lot of memory) :/

  16. jorge-d commented on Apr 14, 2015

    @jorge-d
    Contributor

    @yorkie All the magic is happening here :)

  17. jshkurti commented on Apr 14, 2015

    @jshkurti
    Contributor

    It's just a worker which checks every 30sec the memory usage of your app and restarts it if it exceeds the limit

  18. jshkurti commented on Jun 5, 2015

    @jshkurti
    Contributor

    @ablankenship10 PM2 v0.12.16 is released. This bug shouldn't happen anymore. At least it won't crash PM2 if ENOMEM occurs.

    https://github.com/Unitech/PM2/releases/tag/0.12.16

  19. akamanocha commented on Nov 24, 2016

    @akamanocha

    What is the default value of max-memory-restart?

    We are seeing that in a nodejs app we have given the max-old-space to be 512M its not hitting that limit. Still pm2 is retsrating our app.

    Does pm2 restart on cpu reaching 100% or anything too?

  20. soyuka commented on Nov 24, 2016

    @soyuka
    Contributor

    Does pm2 restart on cpu reaching 100% or anything too?
    Nope.

    What is the default value of max-memory-restart?
    There is none, it is the nodejs default max-old-space-size

    Btw, in nodejs 7 I see:

      --max_old_space_size (max size of the old space (in Mbytes))
            type: int  default: 0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions