35

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

For example:
    USER    USAGE
    root    40370
    admin   247372
    user2   30570
    user3   967373
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Hamidreza
  • 1,825
  • 4
  • 29
  • 40

5 Answers5

45

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
Joshua Huber
  • 3,443
  • 20
  • 27
  • Why "+0.5" though? – Daniel Sep 22 '17 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. – Joshua Huber Sep 22 '17 at 16:59
  • Is it possible to select only processes running with a specific COMMAND (say R)? – utobi Nov 04 '18 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` – Joshua Huber Nov 07 '18 at 14:38
  • it doesn't work. It counts other things that are not R processes. e.g. a user is currently running 9 R processes but the command you suggest geves 364. I tried with `ps hax -o rss,user,command | grep exec/R | 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` but still is not fully reliable... – utobi Nov 08 '18 at 09:58
  • @utobi, Analyzing... what does `pidof R` show. Does this correctly identify just your R processes? – Joshua Huber Nov 08 '18 at 17:12
  • It lists 40 PID which actually correspond to R processes. The number seems correct. I've lunched again your suggestion in the previous comment and it seems to give the RAM used by each user (perhaps due to the R processes) but not counts of R processes. – utobi Nov 09 '18 at 10:43
  • @utobi So are you looking for the number of R processes and total RAM used by them, grouped per user? Like for some server where multiple developers run R under their own accounts? – Joshua Huber Nov 09 '18 at 17:08
  • 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` – Joshua Huber Nov 10 '18 at 01: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.

Alberto Chiusole
  • 2,204
  • 2
  • 16
  • 26
scai
  • 20,297
  • 4
  • 56
  • 72
  • It works but not fine. My memory usage is full (99%) but sum of the your command result show less than 15% – Hamidreza Jan 08 '13 at 12:08
  • Where did you get your *99%* from? – scai Jan 08 '13 at 12:11
  • I'm using `free` command and it show me `total:2047372` `used:2014592` – Hamidreza Jan 08 '13 at 12:13
  • You are interpreting the output of `free` wrong, please read the answers to [similar](http://stackoverflow.com/questions/3784974/want-to-know-whether-enough-memory-is-free-on-a-linux-machine-to-deploy-a-new-ap) [questions](http://serverfault.com/questions/85470/meaning-of-the-buffers-cache-line-in-the-output-of-free) (keywords: buffers/cache). The output of my answer is correct. – scai Jan 08 '13 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. – Joshua Huber Jan 28 '15 at 14:23
  • What about memory shared between processes? Cannot this approach sum some memory bytes twice? – Adam Bajger May 16 '21 at 19:39
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
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
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
Pradeep Pathak
  • 444
  • 5
  • 6
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)))
  • 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. – Moritz 'e1mo' Fromm Jan 18 '21 at 15:24