Hamidreza Hamidreza - 1 year ago 108
Linux Question

How to find user memory usage in linux

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

For example:
root 40370
admin 247372
user2 30570
user3 967373

Answer Source

per-user memory usage in percent using standard tools:

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

or for more precision:

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download