Thursday, June 6, 2013

Linux RAM usage of process by name

Ref: http://abdussamad.com/archives/488-Memory-usage-of-a-process-under-Linux.html

To find the RAM usage of process by name, one can use command like below

---------------


ps -C  -O rss

--------------

For example to find memory usage of google chrome, use command like below.


ps -C httpd -O rss
Sample output would be like below...
PID   RSS S TTY          TIME COMMAND
3299 149484 S ?       00:01:57 /opt/google/chrome/chrome       
3310  7984 S ?        00:00:01 /opt/google/chrome/chrome       
3315 14564 S ?        00:00:00 /opt/google/chrome/chrome --type=zygote
3320 11112 S ?        00:00:00 /opt/google/chrome/chrome --type=zygote
3393 41984 S ?        00:00:00 /opt/google/chrome/chrome --type=renderer 

But to find cumulative/sum of RAM usage of all those process, we can have small shell script which do the job for you.


#!/bin/bash
ps -C $1 -O rss | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =",count; print "Memory usage per process =",sum/1024/count, "MB"; print "Total memory usage =", sum/1024, "MB" ;};'
Save it as psmem.sh and run it like this:
[admin@serve3 ~]$ psmem httpd
Number of processes = 3
Memory usage per process = 9.83464 MB
Total memory usage = 29.5039 MB

No comments: