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:
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.
Do you VM's have the same number of processor's as the host system? if so this is a bad thing, and it can prevent the scheduler from working properly. IE if you have an 8 core system then no system on that box should have 8 cores assigned to it. You can have 20 VM's with 4 Cores assigned and that is not a problem, but 1 box with 8 cores assigned can cause problems under load.
Best Answer
Easily.
Physical memory that is shared between processes - either IPCS shared memory or some of the
mmap()
'd physical memory used by shared objects - will be counted in the RSS of every process that maps that memory into its address space. There's no easy way to account for this when the starting point for counting memory usage is enumerating processes - you'd have to do a virtual-to-physical mapping for all the virtual pages for each process then eliminate duplicates while hoping the memory mappings being examined didn't change while totals were being calculated.A good example of this is an Oracle database server with a large SGA - which is implemented on Solaris using IPCS shared memory. If 20 processes map a 32 GB shared memory SGA, that would contribute 640 GB to the sum of the processes' RSS, even though only 32 GB of RAM is used.