如何在linux中查找用户内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14214315/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find user memory usage in linux
提问by Hamidreza
How i can see memory usage by user in linux centos 6
我如何在 linux centos 6 中查看用户的内存使用情况
For example:
    USER    USAGE
    root    40370
    admin   247372
    user2   30570
    user3   967373
采纳答案by scai
per-user memory usage in percent using standard tools:
使用标准工具的每用户内存使用百分比:
for USER in $(ps haux | awk '{print }' | sort -u)
do
    ps haux | awk -v user=$USER ' ~ user { sum += } END { print user, sum; }'            
done
or for more precision:
或者为了更精确:
TOTAL=$(free | awk '/Mem:/ { print  }')
for USER in $(ps haux | awk '{print }' | sort -u)
do
    ps hux -U $USER | awk -v user=$USER -v total=$TOTAL '{ sum +=  } 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.
第一个版本只是总结了每个进程的内存百分比,如ps. 第二个版本以字节为单位总结内存,然后计算总百分比,从而导致更高的精度。
回答by Audrius Meskauskas
If your system supports, try to install and use smem:
如果您的系统支持,请尝试安装和使用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
在 Ubuntu 中,可以通过键入来安装 smem
sudo apt install smem
回答by Joshua Huber
This one-liner worked for me on at least four different Linux systems with different distros and versions. It also worked on FreeBSD 10.
这个单行程序在至少四个不同发行版和版本的 Linux 系统上对我有用。它也适用于 FreeBSD 10。
ps hax -o rss,user | awk '{a[]+=;}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 awkto do the grouping & summation.
关于实现,这里没有shell循环结构;这使用关联数组进行awk分组和求和。
Here's sample output from one of my servers that is running a decent sized MySQL, Tomcat, and Apache. Figures are in MB.
下面是我的一台服务器的示例输出,该服务器运行了相当规模的 MySQL、Tomcat 和 Apache。数字以 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.
警告:像大多数类似的解决方案一样,这只是考虑了常驻集 (RSS),因此它不计算任何共享内存段。
EDIT: A more human-readable version.
编辑:一个更易读的版本。
echo "USER                 RSS      PROCS" ; echo "-------------------- -------- -----" ; ps hax -o rss,user | awk '{rss[]+=;procs[]+=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
回答by Hridoy Sankar Dutta
You can use the following Python script to find per-user memory usage using only sys and os module.
您可以使用以下 Python 脚本仅使用 sys 和 os 模块查找每个用户的内存使用情况。
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)))
回答by Pradeep Pathak
This will return the total ram usage by users in GBs, reverse sorted
这将返回用户的总内存使用量(以 GB 为单位),反向排序
sudo ps --no-headers -eo user,rss | awk '{arr[]+=}; END {for (i in arr) {print i,arr[i]/1024/1024}}' | sort -nk2 -r

