Finding Apache’s Memory Usage
I was doing some work optimizing the memory usage and performance of Apache and PHP. Along the way, I hacked this tiny CLI PHP script (which in turns uses awk, grep, ps, xargs and pmap) to find out the average private memory consumption of all Apache processes. Very useful when you want to see how much RAM your binaries eat before and after handling requests (they tend to grow, that is why MaxRequestsPerChild is so important…)
You can get the script here. It can be executed either by chmodding it executable (chmod a+x) and running it (if you have a PHP binary in $PATH) or by running `php ./memusage.php`. It might give more accurate results when executed as root, due to the nature of pmap and memory access restrictions.
You might also have to change the value of $apache_proc to match your Apache binary name (for example on Debian and Gentoo that might be ‘apache2′ instead of ‘httpd’).
It works by finding the PIDs of all child Apache processes, running them through `pmap -d` and averaging the results. I bet an experienced bash hacker could have done it all in one line with no PHP wrapper, but I just couldn’t find a way to convert the output of pmap into proper numbers (without the “K” suffix), so it was easier to do the math in PHP.
Needless to say, this is a *nix script, and was tested with Debian Linux. If you’re a Windows user, you
should start looking for a $50 nagware that does the same, or even better, start migrating now













