How to reduce memory usage on a Unix webserver

Thanks everyone for your answers! Following your suggestions I've been able to reduce my memory usage to 195M SWAP and 108M RSS, without touching my code (I'll definitely optimize it soon, but this was supposed to be a solution to get me out of trouble fast).

Here's the list of things I did:

Got rid of the wildcard used in VirtualHost entries. Instead of *:80 and *:443, I used the real IP of my server.

Changed Apache's prefork MPM. These are the values I ended up using:

StartServers           1
MinSpareServers        1 
MaxSpareServers        5 
ServerLimit           16
MaxClients            16
MaxRequestsPerChild    0
ListenBacklog        100

These are by no means magical numbers. I've spent some time trying different values and combination, and then testing them against the real usage of my server and everyone should do the same in their enviroment. For the record, my server receives close to 2M pvs/month, serving both dynamic pages and assets at a regular rate - no digg effect. The intention, again, was to reduce the memory footprint, not to improve performance or HA.

Reference:

  • http://httpd.apache.org/docs/2.0/misc/perf-tuning.html
  • http://httpd.apache.org/docs/2.2/mod/mpm_common.html

Tunned down Apache's KeepAlive. By setting KeepAliveTimeout to a lower value (2 in my case) I can expect less server processes just waiting on connections with idle clients that may not request any more content.

Reference: http://httpd.apache.org/docs/2.0/mod/core.html#keepalivetimeout

Removed MySQL's unused module. I added skip-innodb to MySQL's my.cnf. Massive memory consumption reduction.


There are also some remarkable good suggestions that I couldn't personally do:

  • Remove PHP modules you do not need. The PHP on my server has most mods already compiled, I'll probably try my own minimal PHP on other VPS.
  • Switch to nginx with php-fastcgi. That's another good advice that I'll be trying soon, but right now I can't risk the downtime.

I found this article on low-memory configurations for Apache and MySQL

To be very useful in laying out the configuration changes needed for low memory configurations. I tweaked them for my own situation but they should give you the tools needed to find the best fit for your environment