Hamidreza
Hamidreza

Reputation: 1915

How to find user memory usage in linux

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

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

Upvotes: 36

Views: 55764

Answers (5)

scai
scai

Reputation: 21469

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.

Upvotes: 31

Pradeep Pathak
Pradeep Pathak

Reputation: 454

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

Upvotes: 4

Hridoy Sankar Dutta
Hridoy Sankar Dutta

Reputation: 1

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)))

Upvotes: -1

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

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

Upvotes: 17

Joshua Huber
Joshua Huber

Reputation: 3533

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

Upvotes: 47

Related Questions