36

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

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

5 Answers 5

46

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
9
  • Why "+0.5" though?
    – Daniel
    Sep 22, 2017 at 16:15
  • 3
    @Daniel, awk's int( ) function is a floor function. Adding the 0.5 makes it do proper rounding. So if a process is using 3.9 MB, int(3.9) would display as only 3. But int(3.9+0.5) = 4. Sep 22, 2017 at 16:59
  • Is it possible to select only processes running with a specific COMMAND (say R)?
    – utobi
    Nov 4, 2018 at 20:10
  • 1
    @utobi Try ps --no-headers -o rss,user $(pidof R) | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | sort -rnk2 Nov 7, 2018 at 14:38
  • 1
    @utobi, try echo "USER RSS PROCS" ; echo "-------------------- -------- -----" ; pidof R | while read PID ; do ps --no-header -o rss,user -p $PID ; done | 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 Nov 10, 2018 at 1:32
31

Per-user memory usage in percent using standard tools:

for _user in $(ps haux | awk '{print $1}' | sort -u)
do
    ps haux | awk -v user=${_user} '$1 ~ user { sum += $4} END { print user, sum; }'            
done

or for more precision:

TOTAL=$(free | awk '/Mem:/ { print $2 }')
for _user in $(ps haux | awk '{print $1}' | sort -u)
do
    ps hux -U ${_user} | awk -v user=${_user} -v total=$TOTAL '{ sum += $6 } END { printf "%s %.2f\n", user, sum / total * 100; }'
done

The first version just sums up the memory percentage for each process as reported by ps. The second version sums up the memory in bytes instead and calculates the total percentage afterwards, thus leading to a higher precision.

6
  • It works but not fine. My memory usage is full (99%) but sum of the your command result show less than 15%
    – Hamidreza
    Jan 8, 2013 at 12:08
  • Where did you get your 99% from?
    – scai
    Jan 8, 2013 at 12:11
  • I'm using free command and it show me total:2047372 used:2014592
    – Hamidreza
    Jan 8, 2013 at 12:13
  • You are interpreting the output of free wrong, please read the answers to similar questions (keywords: buffers/cache). The output of my answer is correct.
    – scai
    Jan 8, 2013 at 12:16
  • 2
    Good logic, but my ps on SUSE 13.2 did weird things with long usernames, eg "garfieldthecat" would show up as "garfiel+", and then the summing wouldn't work. Also beware that $USER is a built-in variable for the current user in most shells. Jan 28, 2015 at 14:23
17

If your system supports, try to install and use smem:

smem -u

User     Count     Swap      USS      PSS      RSS 
gdm          1        0      308      323      820 
nobody       1        0      912      932     2240 
root        76        0   969016  1010829  1347768 

or

smem -u -t -k

User     Count     Swap      USS      PSS      RSS 
gdm          1        0   308.0K   323.0K   820.0K 
nobody       1        0   892.0K   912.0K     2.2M 
root        76        0   937.6M   978.5M     1.3G 
ameskaas    46        0     1.2G     1.2G     1.5G 

           124        0     2.1G     2.2G     2.8G 

In Ubuntu, smem can be installed by typing

sudo apt install smem
3

This will return the total ram usage by users in GBs, reverse sorted

sudo ps --no-headers -eo user,rss | awk '{arr[$1]+=$2}; END {for (i in arr) {print i,arr[i]/1024/1024}}' | sort -nk2 -r
0

You can use the following Python script to find per-user memory usage using only sys and os module.

import sys
import os

# Get list of all users present in the system
allUsers = os.popen('cut -d: -f1 /etc/passwd').read().split('\n')[:-1]

for users in allUsers:
    # Check if the home directory exists for the user
    if os.path.exists('/home/' + str(users)):
        # Print the current usage of the user
        print(os.system('du -sh /home/' + str(users)))
1
  • I appears you confused memory usage with storage usage. Memory usage refers to RAM while the solution you provided accounts for the space used on disk. Jan 18, 2021 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.