How to count the private memory used by a program

After reading the post on Understanding memory usage on Linux, I wanted a simple method to count the private bytes taken eg. by apache or php processes. Writing this is really not a big deal, but might be helpful for newbies ...

Eg. count the private bytes used by the PHP5 CGI processes:
memtotal=0
memcount=0
for pid in `ps -C php5-cgi -o pid=`; do
  mem=$(pmap -d ${pid} | grep 'writeable/private' | sed -e 's/.*writeable\/private: *\([0-9]*\)K.*/\1/')
  memtotal=$((memtotal+mem))
  memcount=$((memcount+1))
done
echo -e "Total: ${memtotal}\nCount: ${memcount}"

You can put this into a shellscript (maybe modify it a bit so you can supply an arbitrary process name as a parameter) or run it as a one-liner (originally it was a one-liner, but I've split it into multiple lines for readability).

Syndicate content