如何查看按实际内存使用情况排序的顶级进程?
我有一台12G内存的服务器。 top 的片段如下所示:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12979 frank 20 0 206m 21m 12m S 11 0.2 26667:24 krfb
13 root 15 -5 0 0 0 S 1 0.0 36:25.04 ksoftirqd/3
59 root 15 -5 0 0 0 S 0 0.0 4:53.00 ata/2
2155 root 20 0 662m 37m 8364 S 0 0.3 338:10.25 Xorg
4560 frank 20 0 8672 1300 852 R 0 0.0 0:00.03 top
12981 frank 20 0 987m 27m 15m S 0 0.2 45:10.82 amarok
24908 frank 20 0 16648 708 548 S 0 0.0 2:08.84 wrapper
1 root 20 0 8072 608 572 S 0 0.0 0:47.36 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
free -m
显示以下内容:
total used free shared buffers cached
Mem: 12038 11676 362 0 599 9745
-/+ buffers/cache: 1331 10706
Swap: 2204 257 1946
如果我理解正确的话,系统只有 362 MB 的可用内存。我的问题是:如何找出哪个进程消耗了大部分内存?
正如背景信息一样,系统正在运行 64 位 OpenSuse 12
。
I have a server with 12G of memory. A fragment of top is shown below:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12979 frank 20 0 206m 21m 12m S 11 0.2 26667:24 krfb
13 root 15 -5 0 0 0 S 1 0.0 36:25.04 ksoftirqd/3
59 root 15 -5 0 0 0 S 0 0.0 4:53.00 ata/2
2155 root 20 0 662m 37m 8364 S 0 0.3 338:10.25 Xorg
4560 frank 20 0 8672 1300 852 R 0 0.0 0:00.03 top
12981 frank 20 0 987m 27m 15m S 0 0.2 45:10.82 amarok
24908 frank 20 0 16648 708 548 S 0 0.0 2:08.84 wrapper
1 root 20 0 8072 608 572 S 0 0.0 0:47.36 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
The free -m
shows the following:
total used free shared buffers cached
Mem: 12038 11676 362 0 599 9745
-/+ buffers/cache: 1331 10706
Swap: 2204 257 1946
If I understand correctly, the system has only 362 MB of available memory. My question is: How can I find out which process is consuming most of the memory?
Just as background info, the system is running 64bit OpenSuse 12
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用快速提示,在 linux/unix 中使用 top 命令
,然后按 Shift+m (即写一个大写的
M
)。来自
man top
或者:按 Shift + f ,然后按 nnnn 键按内存使用情况排序显示kbd> 然后按 Enter。您将看到按内存使用情况排序的活动进程
use quick tip using top command in linux/unix
and then hit Shift+m (i.e. write a capital
M
).From
man top
Or alternatively: hit Shift + f , then choose the display to order by memory usage by hitting key n then press Enter. You will see active process ordered by memory usage
首先,重复一下这句口头禅:“未使用的内存就是浪费的内存”。 Linux 内核保留大量的文件元数据和请求的文件,直到看起来更重要的东西将这些数据推出。这就是为什么你可以运行:
并让第二个
find
实例以惊人的速度运行。Linux 只留下一点点“空闲”内存来处理内存使用高峰,而无需付出太多努力。
其次,您想要找到正在消耗您所有内存的进程;在
top
中使用M
命令按内存使用情况排序。请忽略 VIRT 列,它只是告诉您已分配了多少虚拟内存,而不是进程正在使用多少内存。RES
报告有多少内存常驻,或当前在 RAM 中(而不是交换到磁盘或从未实际分配过,尽管已被请求)。但是,由于
RES
会为几乎每个进程计算一次/lib/libc.so.6
内存,因此它并不是一个衡量进程内存大小的绝佳方法正在使用。SHR
列报告与其他进程共享多少内存,但不能保证另一个进程实际上正在共享 - 它可能是可共享的,只是没有其他人想要分享。smem
工具旨在帮助用户更好地衡量实际应该使用多少内存 /em> 归咎于每个单独的进程。它做了一些聪明的工作来找出什么是真正独特的,什么是共享的,并按比例计算共享内存与共享它的进程。smem
可能会比top
更好地帮助您了解记忆的进展情况,但top
是一个出色的首选工具。First, repeat this mantra for a little while: "unused memory is wasted memory". The Linux kernel keeps around huge amounts of file metadata and files that were requested, until something that looks more important pushes that data out. It's why you can run:
and have the second
find
instance run at ridiculous speed.Linux only leaves a little bit of memory 'free' to handle spikes in memory usage without too much effort.
Second, you want to find the processes that are eating all your memory; in
top
use theM
command to sort by memory use. Feel free to ignore theVIRT
column, that just tells you how much virtual memory has been allocated, not how much memory the process is using.RES
reports how much memory is resident, or currently in ram (as opposed to swapped to disk or never actually allocated in the first place, despite being requested).But, since
RES
will count e.g./lib/libc.so.6
memory once for nearly every process, it isn't exactly an awesome measure of how much memory a process is using. TheSHR
column reports how much memory is shared with other processes, but there is no guarantee that another process is actually sharing -- it could be sharable, just no one else wants to share.The
smem
tool is designed to help users better gage just how much memory should really be blamed on each individual process. It does some clever work to figure out what is really unique, what is shared, and proportionally tallies the shared memory to the processes sharing it.smem
may help you understand where your memory is going better thantop
will, buttop
is an excellent first tool.(将 -n 数字标志添加到排序命令。)
(Adding -n numeric flag to sort command.)
首先,您应该阅读 关于
free
输出的解释。底线:您至少有 10.7 GB 可供进程使用的内存。然后,您应该定义进程的“内存使用量”(这并不容易或明确,相信我)。
那么我们也许能够提供更多帮助:-)
First you should read an explanation on the output of
free
. Bottom line: you have at least 10.7 GB of memory readily usable by processes.Then you should define what "memory usage" is for a process (it's not easy or unambiguous, trust me).
Then we might be able to help more :-)
ps aux --sort '%mem'
来自 procps' ps(Ubuntu 12.04 上的默认设置)生成如下输出:
所以这里 Firefox 是最大的消耗者,占用了我的 16% 的内存。
您可能还对:
ps aux --sort '%mem'
from procps' ps (default on Ubuntu 12.04) generates output like:
So here Firefox is the top consumer with 16% of my memory.
You may also be interested in:
按内存使用情况列出和排序进程:
List and Sort Processes by Memory Usage:
基于 gaoithe 答案,我尝试使内存单位以兆字节为单位显示,并按内存降序排序,限制为 15 个条目:
ps -e -orss=,args= |awk '{print $1 " " $2 }'| awk '{tot[$2]+=$1;count[$2]++} END {for (i in tot) {print tot[i],i,count[i]}}' |排序 -n |尾-n 15 |排序-nr | awk '{ hr=$1/1024; printf("%13.2fM", 小时); print "\t" $2 }'
下面是在 bash 配置文件中使用它的示例别名:
然后您只需在命令行中键入
topmem
即可。Building on gaoithe's answer, I attempted to make the memory units display in megabytes, and sorted by memory descending limited to 15 entries:
ps -e -orss=,args= |awk '{print $1 " " $2 }'| awk '{tot[$2]+=$1;count[$2]++} END {for (i in tot) {print tot[i],i,count[i]}}' | sort -n | tail -n 15 | sort -nr | awk '{ hr=$1/1024; printf("%13.2fM", hr); print "\t" $2 }'
Here's an example alias to use it in a bash config file:
Then you can just type
topmem
on the command line.你有这个简单的命令:
You have this simple command:
您可以通过以下步骤指定要排序的列:
you can specify which column to sort by, with following steps:
您可以通过在终端中执行以下代码来查看内存使用情况:
You can see memory usage by executing this code in your terminal:
就在这一秒
不断更新
我还在这里添加了一些您可能会欣赏(或者您可能会忽略)的好东西
-n 1
每秒观看并更新-U $(whoami)
仅显示您的进程。 $(some command) 现在计算| head -n4
一次只显示标题和 3 个进程,因为通常您只需要高使用率行项目${1-4}
说我的第一个参数$1 我想默认为 4,除非我提供它
如果您使用的是 Mac,您可能需要先安装 watch
brew install watch
或者你可以使用一个函数
This very second in time
Continuously updating
I also added a few goodies here you might appreciate (or you might ignore)
-n 1
watch and update every second-U $(whoami)
To show only your processes. $(some command) evaluates now| head -n4
To only show the header and 3 processes at a time bc often you just need high usage line items${1-4}
says my first argument$1
I want to default to 4, unless I provide itIf you are using a mac you may need to install watch first
brew install watch
Alternatively you might use a function
如何按进程名称合计已用内存:
有时,即使查看最大的单个进程,仍然有大量已用内存未计算在内。要检查是否有许多相同的较小进程在使用内存,您可以使用如下命令,该命令使用 awk 来总结同名进程使用的总内存:
例如输出
How to total up used memory by process name:
Sometimes even looking at the biggest single processes there is still a lot of used memory unaccounted for. To check if there are a lot of the same smaller processes using the memory you can use a command like the following which uses awk to sum up the total memory used by processes of the same name:
e.g. output