Unix Processes Memory Usage

I’m not a sys admin, but sometimes I need to profile applications in development or production environments. When this happens I realize that I don’t remember those large -very large- commands using awk and prints everywhere.

Today was one of that days, so I decided to write this post as a reference for myself.

Total memory usage:

$> ps aux |  awk '{print $6/1024}' | awk '{sum += $1}; END {print sum " MB"}'

Total memory usage by process, for example Apache:

$> ps aux | grep apache2 | awk '{print $6/1024}' | awk '{sum += $1}; END {print sum " MB"}'

Average memory used by a process (if it uses childs):

$> ps aux | grep apache2 | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}'

All of them use process’s resident memory (not virtual memory).

That’s all.