Recently we need to find RAM(aka Physical memory) used by a User.. I thought it’s bit easy to find RAM usage by using a group of commands and chaining them to get desired output. Let me explain what I did to get RAM usage of a user and came to know it’s blunder mistake to go in that way to find the RAM use by a user. Join me to dig in to this and which led to find real RAM used by a user with other command called smem

Commands used by me to find the user RAM utilization earlier are as follows..

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

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}'

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


	smem -u surendra
User     Count     Swap      USS      PSS      RSS
surendra    60        0   703132   730122 1051560

Note:The values are in KB

So real RAM utilised by user Surendra is 730MB(PSS value).

Even you can add USS, PSS, RSS which will give us total Memory used by user here it’s 12.95GB which we get in our chained commands.

Please share your thoughts on this, if you have other means to check RAM used by a user/application.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.