Ubuntu – ny way to automatically prevent running out of memory

apache-2.2memoryUbuntuvps

I am often running out of memory on my VPS ubuntu server. I wish there was a way to simply restart apache2 when it starts running out of memory, as that seems to solve the problem. Or am I just too lazy to fix the problem? I do have limited memory on the server…

Okay, more information:

I'm running apache2 prefork, here are my memory settings (i've been tweaking them…):

StartServers         3
MinSpareServers      1
MaxSpareServers      5
MaxClients           150
MaxRequestsPerChild  1000

The VPS has 1 GB of ram, running ubuntu 11.04 32-bit.

As for scripts, I have a wordpress network with 5 blogs, an install of AskBot (a python/django stackexchange clone), and an install of MediaWiki that isn't really used. There is also a homebrewed mp3 script that accesses the getid3 library to display information on lists of podcasts, and it seems to be throwing some php errors, not sure if that's the culprit…

Best Answer

Monit can do this.

You could use a config line such as:

 check process apache 
    with pidfile "/var/lock/apache/httpd.pid"
    start program = "/etc/init.d/httpd start" with timeout 60 seconds
    stop program = "/etc/init.d/httpd stop"
    if 2 restarts within 3 cycles then timeout
    if totalmem > 100 Mb for 5 cycles then restart
    if children > 255 for 5 cycles then stop
    if cpu usage > 95% for 3 cycles then restart
    if failed port 80 protocol http then restart
    group server
    depends on httpd.conf, httpd.bin

(Modified from this config example on monit.com)

This could play into the ulimit option mentioned earlier as well.

Restarting the service is a bandaid. You should instead try to find out why it's leaking memory.