Finding Apache Average Memory Usage on Debian Wheezy

I wanted to find the average process memory usage on Debian wheezy to calculate MaxClients.

I used this script to do it:

sudo ps -ef | grep apache2 | grep -v ^$USER | awk '{ print $2 '} | xargs sudo pmap -d | grep ^mapped: | awk '{ print $4 }' | cut -dK -f1 | awk '{SUM += $1} END { print SUM/NR }'

Quick explanation, starts out with a process list, filters for apache2, removes the grep command from the list, prints the second column, uses pmap to display memory usage, then grep to filter out the mapped memory. Again printing the fourth column, then using cut to remove the K, and awk to sum up the records. NR in awk is the number of input records seen so far.

To get MaxClients divide your total memory (minus some margin for other system processes) by the average process memory usage to get the number for MaxClients.

Example, 50K process size, memory of 1.8G, 1.8G/50K = 36

Note you shouldn’t use the ps VSZ as it includes shared memory.