The VmPeak answer looks like a good one to me... You could either append vmpeak to a file every x seconds and then find the highest value. Or, every x seconds, run something like: VmPeak = curVmPeak if (curVmPeak > VmPeak)
.
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.
Best Answer
You can get the peak memory usage of a certain process, at:
(Change $PID to the actual process id you're looking for).
VmPeak is the maximum amount of memory the process has used since it was started.
In order to track the memory usage of a process over time, you can use a tool called munin to track, and show you a nice graph of the memory usage over time.
Munin comes with many default plugins to track system resources, however it doesn't come with a plugin to track Peak memory usage - fortunetly, its extremely easy to write a plugin for it.
Here's an example of a munin plugin to track VmPeak,VmRSS and VmSize memory usage , for the apache process. You can change this to suit your needs (just point to the right PID file and change the component name as needed).
The graph it outputs looks like this (VmPeak and VmSize are the same in this example, so you only see one of them):
Note: this only monitors the main apache process, and doesn't show the memory usage of it's child processes.