Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 950
Hamidreza Avatar asked Jan 08 '13 11:01

Hamidreza


People also ask

How do I check my memory usage?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.

How do I check memory usage on PID?

The ps command can also be used to monitor memory usage of individual processes. The ps v PID command provides the most comprehensive report on memory-related statistics for an individual process, such as: Page faults. Size of working segment that has been touched.

How do I find my highest memory utilization on Linux?

Use ps Command to Find Top Processes by Memory and CPU Usage ps is a Linux command-line utility with many options that helps you to display output in different formats. You can use the ps command with –sort argument to sort the output by memory and CPU usage.


2 Answers

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 
like image 113
Joshua Huber Avatar answered Oct 16 '22 09:10

Joshua Huber


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.

like image 41
scai Avatar answered Oct 16 '22 08:10

scai