如何查看按实际内存使用情况排序的顶级进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802481/
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 see top processes sorted by actual memory usage?
提问by user3111525
I have a server with 12G of memory. A fragment of top is shown below:
我有一台内存为 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
The free -m
shows the following:
该free -m
显示以下内容:
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?
如果我理解正确,系统只有 362 MB 的可用内存。我的问题是:如何找出哪个进程消耗了大部分内存?
Just as background info, the system is running 64bit OpenSuse 12
.
就像背景信息一样,系统正在运行64bit OpenSuse 12
。
采纳答案by sarnold
First, repeat this mantra for a little while: "unused memory is wasted memory". The Linux kernel keeps around hugeamounts of file metadata and files that were requested, until something that looks more important pushes that data out. It's why you can run:
首先,重复一下这个口头禅:“未使用的记忆就是浪费的记忆”。Linux内核保持周围巨大的文件元数据并要求提供的文件量,直到一些看起来更重要的是推动了数据。这就是为什么你可以运行:
find /home -type f -name '*.mp3'
find /home -type f -name '*.aac'
and have the second find
instance run at ridiculous speed.
并让第二个find
实例以荒谬的速度运行。
Linux only leaves a little bit of memory 'free' to handle spikes in memory usage without too much effort.
Linux 只留下一点点“空闲”内存来处理内存使用高峰,而无需太多努力。
Second, you want to find the processes that are eating all your memory; in top
use the M
command to sort by memory use. Feel free to ignore the VIRT
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).
其次,你想找到吞噬你所有记忆的进程;在top
使用M
命令按内存使用排序。随意忽略该VIRT
列,它只是告诉您已分配多少虚拟内存,而不是进程正在使用多少内存。RES
报告有多少内存是resident,或当前在 ram 中(而不是交换到磁盘或从一开始就没有实际分配,尽管被请求)。
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. The SHR
column reports how much memory is sharedwith other processes, but there is no guarantee that another process is actually sharing -- it could be sharable, just no one else wants to share.
但是,由于几乎每个进程RES
都会计算/lib/libc.so.6
一次内存,因此它并不是衡量进程使用多少内存的好方法。该SHR
列报告与其他进程共享多少内存,但不能保证另一个进程实际上正在共享——它可能是可共享的,只是没有其他人想要共享。
The smem
tool is designed to help users better gage just how much memory should reallybe 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 than top
will, but top
is an excellent first tool.
该smem
工具旨在帮助用户更好地衡量每个进程应该真正归咎于多少内存。它做了一些聪明的工作来弄清楚什么是真正独特的,什么是共享的,并按比例将共享内存与共享它的进程相匹配。smem
可能会帮助您了解您的记忆力在哪里比top
将要好,但它top
是一个很好的第一个工具。
回答by thkala
First you should read an explanation on the output of free
. Bottom line: you have at least 10.7 GBof memory readily usable by processes.
首先,您应该阅读有关free
. 底线:您至少有10.7 GB的内存可供进程轻松使用。
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 :-)
那么我们也许可以提供更多帮助:-)
回答by risnandar
use quick tip using top command in linux/unix
在 linux/unix 中使用 top 命令使用快速提示
$ top
and then hit Shift+m(i.e. write a capital M
).
然后点击Shift+ m(即写一个大写M
)。
From man top
从 man top
SORTING of task window
For compatibility, this top supports most of the former top sort keys.
Since this is primarily a service to former top users, these commands do
not appear on any help screen.
command sorted-field supported
A start time (non-display) No
M %MEM Yes
N PID Yes
P %CPU Yes
T TIME+ Yes
Or alternatively: hit Shift+ f, then choose the display to order by memory usage by hitting key nthen press Enter. You will see active process ordered by memory usage
或者:点击Shift+ f,n然后按键选择要按内存使用情况排序的显示,然后按Enter。您将看到按内存使用情况排序的活动进程
回答by Angelinux
ps aux | awk '{print , , }' | sort -k2rn | head -n 10
(Adding -n numeric flag to sort command.)
(将 -n 数字标志添加到排序命令。)
回答by Tomir Schmite Jr.
List and Sort Processes by Memory Usage:
按内存使用列出和排序进程:
ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS
回答by Eric Wang
you can specify which column to sort by, with following steps:
您可以通过以下步骤指定要排序的列:
steps: * top * shift + F * select a column from the list e.g. n means sort by memory, * press enter * ok
回答by Cubiczx
You can see memory usage by executing this code in your terminal:
您可以通过在终端中执行此代码来查看内存使用情况:
$ watch -n2 free -m
$ htop
回答by Christian IC
You have this simple command:
你有这个简单的命令:
$ free -h
回答by gaoithe
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:
有时即使查看最大的单个进程,仍然有很多未使用的内存。要检查是否有许多相同的较小进程使用内存,您可以使用如下命令,该命令使用 awk 来总结同名进程使用的总内存:
ps -e -orss=,args= |awk '{print " " }'| awk '{tot[]+=;count[]++} END {for (i in tot) {print tot[i],i,count[i]}}' | sort -n
e.g. output
例如输出
9344 docker 1
9948 nginx: 4
22500 /usr/sbin/NetworkManager 1
24704 sleep 69
26436 /usr/sbin/sshd 15
34828 -bash 19
39268 sshd: 10
58384 /bin/su 28
59876 /bin/ksh 29
73408 /usr/bin/python 2
78176 /usr/bin/dockerd 1
134396 /bin/sh 84
5407132 bin/naughty_small_proc 1432
28061916 /usr/local/jdk/bin/java 7