How to find user memory usage in linux

Hamidreza picture Hamidreza · Jan 8, 2013 · Viewed 48.8k times · Source

How i can see memory usage by user in linux centos 6

For example:
    USER    USAGE
    root    40370
    admin   247372
    user2   30570
    user3   967373

Answer

Joshua Huber picture Joshua Huber · Jan 28, 2015

This one-liner worked for me on at least four different Linux systems with different distros and versions. It also worked on FreeBSD 10.

ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | sort -rnk2

About the implementation, there are no shell loop constructs here; this uses an associative array in awk to do the grouping & summation.

Here's sample output from one of my servers that is running a decent sized MySQL, Tomcat, and Apache. Figures are in MB.

mysql 1566
joshua 1186                                                                                  
tomcat 353                                                                                   
root 28                                                                                      
wwwrun 12                                                                                    
vbox 1                                                                                       
messagebus 1                                                                                 
avahi 1                                                                                      
statd 0                                                                                      
nagios 0

Caveat: like most similar solutions, this is only considering the resident set (RSS), so it doesn't count any shared memory segments.

EDIT: A more human-readable version.

echo "USER                 RSS      PROCS" ; echo "-------------------- -------- -----" ; ps hax -o rss,user | awk '{rss[$2]+=$1;procs[$2]+=1;}END{for(user in rss) printf "%-20s %8.0f %5.0f\n", user, rss[user]/1024, procs[user];}' | sort -rnk2

And the output:

USER                 RSS      PROCS
-------------------- -------- -----
mysql                    1521     1
joshua                   1120    28
tomcat                    379     1
root                       19   107
wwwrun                     10    10
vbox                        1     3
statd                       1     1
nagios                      1     1
messagebus                  1     1
avahi                       1     1