如何使用 free -m 获取所有用户的内存使用情况

Aka*_*ari 4 command-line memory-usage 12.04 free

我输入了 free 命令来获取内存使用情况,如下所示:

free -m 
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

我想使用此命令获取相同的信息,但对于所有用户,例如:

在此处输入图片说明

我使用了这个命令 .. 因为它对我来说很容易 .. 将它的输出存储在 bash 脚本的变量中......

ger*_*ijk 5

一种选择是使用 smem 安装smemasfree不提供此功能。

$ sudo smem -u -k -t
User     Count     Swap      USS      PSS      RSS 
daemon       1        0   196.0K   197.0K   360.0K 
rtkit        1        0   304.0K   317.0K     1.4M 
[...]
root        44        0   164.3M   197.7M   284.4M 
gert        88        0     1.7G     1.8G     3.2G 
---------------------------------------------------
           159        0     1.9G     2.1G     3.6G
Run Code Online (Sandbox Code Playgroud)

有关 USS 和 PSS 含义的解释,这里是 smem 联机帮助页的摘录。

            [...] Unshared memory is reported as the USS (Unique Set
   Size).  Shared memory is divided evenly among the processes shar?
   ing that memory.  The unshared memory (USS) plus a process's pro?
   portion of shared memory is reported as the PSS (Proportional Set
   Size).  The USS and PSS only include physical memory usage.  They
   do not include memory that has been swapped out to disk.
Run Code Online (Sandbox Code Playgroud)

怀疑RSS 是住宅内存使用量,在其他实用程序中也称为 RES。有关内存使用情况表达的更多信息,请参阅 Superuser.com 上的此问答:关于内存管理,我应该了解什么?


Oli*_*Oli 5

free 已经基于系统范围的内存使用情况。

如果您想要基于每个用户的内容,您可以尝试以下操作:

ps aux | awk 'NR>2{arr[$1]+=$6}END{for(i in arr) print i,arr[i]}'
Run Code Online (Sandbox Code Playgroud)

作为对它的作用的快速解释awk

  • 去掉第一行
  • 遍历每一行并创建每个给定用户名的数组。对于其中的每一个,它从ps aux(常驻集大小)中添加第六列并将它们相加。
  • 之后它只是遍历数组键来打印内容。