Sunday, January 27, 2013

How to find RAM utilization by user in Linux.

1)pmap –To map all the RAM used by a process
2)pgrep –To grep/get all the process for a user/Application
3)grep –To filter some data
4)awk to filter some column
For example find the RAM utilised by a user, we used following command
pmap `pgrep -u 1000` | grep total | awk '{print $2}' | awk '{s+=$1}END{print s}'
Let me explain the command
pgrep -u 1000 this command is to get all the process ID for the user whose UID is 1000
Example clipped output
pgrep -u 1000
1602
1621
1651
pmap `pgrep -u 1000` will display all the memory details for the process run
Example clipped output
pmap `pgrep -u 1000`
1602:   /usr/bin/gnome-keyring-daemon --daemonize --login
0000000000400000    840K r-x--  /usr/bin/gnome-keyring-daemon
00000000006d2000     36K r----  /usr/bin/gnome-keyring-daemon
00000000006db000     12K rw---  /usr/bin/gnome-keyring-daemon
00000000006de000      8K rw---    [ anon ]
pmap `pgrep -u 1000` | grep total will grep “total” word from the pmap output.
Example clipped output
pmap `pgrep -u 1000` | grep total | more
 total            84872K
 total           237228K
 total                0K
 total            26308K
 total            25560K
pmap `pgrep -u 1000` | grep total | awk ‘{print $2}’ will display only total memory usage by each process
pmap `pgrep -u 1000` | grep total | awk ‘{print $2}’  | awk ‘{s+=$1}END{print s}’ To add all the RAM displayed by pmap command..
Example output
pmap `pgrep -u 1000` | grep total | awk '{print $2}' | awk '{s+=$1}END{print s}'
12954040
The above value is in KB, So my total RAM used by the user is 12.95GB. Which is blunder mistake as my Total RAM is just 3GB.
#free -m
 total       used       free     shared    buffers     cached
Mem:          2982       2951         30          0        146       1834
-/+ buffers/cache:        970       2011
Swap:         9535          0       9535
then where is this 12.95GB RAM came from?
This is because large portions of RAM shared between different Application using same libraries which will over-estimate the RSS(resident set size). If we need to see real RAM utilisation ie proportional set size (PSS) there is a command smem(Show MEMory) from kernel 2.6.27 to check actual RAM utilised by User.
How to get smem?
On ubuntu based machines:
#apt-get install smem
On Redhat based machines:
#yum install smem
how to use this command
#smem -u username
Example
smem -u XXX
User     Count     Swap      USS      PSS      RSS 
surendra    60        0   703132   730122 1051560 
Note:The values are in KB
So real RAM utilised by user XXX is 730MB(PSS value).