Hamidreza Hamidreza - 5 months ago 21
Linux Question

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

Answer

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.

Comments