5

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...

5 Answers 5

4

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.

1
  • Thank you, this has answered the question directly. I'm not going to implement it, though... I think I've solved the problem by installing php-apc, which enables php page caching. Jul 6, 2011 at 16:25
7

Apache shouldn't be running out of memory, unless you have some memory leak in the application you run under apache, or if a certain request makes a huge demand on memory. I would investigate why I run out of memory in the first place, than implementing a brute force solution like one you ask for.

With above said, it should be very straightforward to implement a basic cron job script, which just calls "ps" on apache processes, finds out how much memory they consume, and restart if needed. Running this every minute should be enough.

But again - this is not a good approach.

2
  • Thanks, I guess I could patch together such a script... but you're probably right, I should figure out the real problem. Jul 1, 2011 at 16:03
  • Good advice: Always go after the cause.
    – pboin
    Jul 1, 2011 at 18:39
3

Running apache under ulimit -m X will cause it to get killed when it (and its children) exceed XK RSS. I don't know if Ubuntu still uses Upstart but if it does then you can add the respawn option to the apache upstart config file to cause it to be restarted automatically after it is killed.

0

This a pretty complex question, actually, because it depends on what is consuming the memory. If you're strictly serving static content (who does this anymore?) then you have to take a look at your Apache config to make sure it's not allowing more concurrent connections/threads than your available memory can handle.

More likely you have some PHP scripts or a poorly tuned backend application that is improperly consuming all of your memory. This happens a lot with large PHP applications that increase their RAM maximum to 32 or 64M on startup and you get 4 visitors at once.

Can you post some additional information on what apache is configured like on your server?

0
0

Another reason you may run out of memory is that you don't have any swap file on your ubuntu server.

Your machine may run out of memory because of some specific request that occurs from time to time which causes lots of memory to be consumed. If you have swap file (should be about twice as big as server memory) it may solve your problem.

But still you need to investigate what causes all memory to be consumed. If memory is consumed all the time and not released, swap file won't help - it may actually make it worse since all memory will get consumed and because of swapping machine will run very-very-very slow.

1
  • As per the question, I'm using a VPS... I don't think a swap file would work. Jul 13, 2011 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .