Get total memory usage of a program

You can use ps to list details of the processes running on your system and awk to filter for a particular program and total the values.

In this case we’ll total the RSS, which is awk’s 6th token hence the $6 in the command. If you wanted to total the %CPU, %MEM, VSZ or any other value provided by ps aux, simply count its column number n and use $n instead.

me@pc ~ $ ps aux | awk '/firefox/ {total+=$6} END {print total}'
965284

One gotcha

When you run the command be aware of which processes are being found by awk. If it’s picking up too many processes then change the /regular expression/ accordingly.

In the example used above, note that the result will include the RSS of the command you just run - because it will match the regexp you’re using - as you can see if you run it without totaling the values:

me@pc ~ $ ps aux | awk '/firefox/'
steph     3993  6.7 20.0 1611068 812984 pts/0  Sl   19:46  14:40 firefox
steph     4039  2.8  3.7 586524 151396 pts/0   Sl   19:47   6:07 /usr/lib64/firefox/plugin-container /opt/Adobe/flash-player/flash-plugin/libflashplayer.so -greomni /usr/lib64/firefox/omni.ja 3993 false plugin
steph     6004  0.0  0.0  10700   900 pts/2    S+   23:24   0:00 awk /firefox/

I don’t know why the total is 4KB too large. It always seems to be.

References

Last modified: 25/04/2012 Tags: ,

Related Pages

Other pages possibly of interest:

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top