在 Linux 中,如何判断进程使用了多少内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3853655/
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
In Linux, how to tell how much memory processes are using?
提问by StackOverflowNewbie
I think I may have a memory leak in my LAMP application (memory gets used up, swap starts getting used, etc.). If I could see how much memory the various processes are using, it might help me resolve my problem. Is there a way for me to see this information in *nix?
我想我的 LAMP 应用程序中可能存在内存泄漏(内存已用完,交换开始被使用等)。如果我能看到各个进程使用了多少内存,它可能会帮助我解决我的问题。有没有办法让我在 *nix 中看到这些信息?
采纳答案by Giuseppe Cardone
Getting right memory usage is trickier than one may think. The best way I could find is:
获得正确的内存使用比人们想象的要棘手。我能找到的最好方法是:
echo 0 $(awk '/TYPE/ {print "+", }' /proc/`pidof PROCESS`/smaps) | bc
Where "PROCESS" is the name of the process you want to inspect and "TYPE" is one of:
其中“PROCESS”是您要检查的过程的名称,“TYPE”是以下之一:
Rss: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;Shared: memory that this process shares with other processes;Private: private memory used by this process, you can look for memory leaks here;Swap: swap memory used by the process;Pss: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiB
Rss:常驻内存使用情况,该进程使用的所有内存,包括该进程与其他进程共享的所有内存。不包括掉期;Shared:这个进程与其他进程共享的内存;Private: 此进程使用的私有内存,可以在这里查找内存泄漏;Swap:交换进程使用的内存;Pss: Proportional Set Size,一个很好的整体内存指标。它是为共享调整的 Rss:如果一个进程有 1MiB 私有和 20MiB 在其他 10 个进程之间共享,则 Pss 是 1 + 20/10 = 3MiB
Other valid values are Size(i.e. virtual size, which is almost meaningless) and Referenced(the amount of memory currently marked as referenced or accessed).
其他有效值是Size(即虚拟大小,几乎没有意义)和Referenced(当前标记为引用或访问的内存量)。
You can use watchor some other bash-script-fu to keep an eye on those values for processes that you want to monitor.
您可以使用watchbash-script-fu 或其他一些 bash-script-fu 来关注要监视的进程的这些值。
For more informations about smaps: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.
有关更多信息smaps:http: //www.kernel.org/doc/Documentation/filesystems/proc.txt。
回答by Gunther Piez
Use topor htopand pay attention to the "RES" (resident memory size) column.
使用top或htop并注意“RES”(常驻内存大小)列。
回答by Zaz
Use psto find the process id for the application, then use top -p1010(substitute 1010 for the real process id).
The RES column is the used physical memory and the VIRT column is the used virtual memory - including libraries and swapped memory.
使用ps找到应用程序的进程ID,然后使用top -p1010(替代1010为真正的进程ID)。RES 列是已使用的物理内存,VIRT 列是已使用的虚拟内存 - 包括库和交换内存。
More info can be found using "man top"
可以使用“man top”找到更多信息
回答by Paul Rubenstein
Thanks. I used this to create this simple bash script that can be used to watch a process and its memory usage:
谢谢。我用它来创建这个简单的 bash 脚本,可用于观察进程及其内存使用情况:
$ watch watchmypid.sh
$ watch watchmypid.sh
#!/bin/bash
#
PROCESSNAME=changethistoyourprocessname
MYPID=`pidof $PROCESSNAME`
echo "=======";
echo PID:$MYPID
echo "--------"
Rss=`echo 0 $(cat /proc/$MYPID/smaps | grep Rss | awk '{print }' | sed 's#^#+#') | bc;`
Shared=`echo 0 $(cat /proc/$MYPID/smaps | grep Shared | awk '{print }' | sed 's#^#+#') | bc;`
Private=`echo 0 $(cat /proc/$MYPID/smaps | grep Private | awk '{print }' | sed 's#^#+#') | bc;`
Swap=`echo 0 $(cat /proc/$MYPID/smaps | grep Swap | awk '{print }' | sed 's#^#+#') | bc;`
Pss=`echo 0 $(cat /proc/$MYPID/smaps | grep Pss | awk '{print }' | sed 's#^#+#') | bc;`
Mem=`echo "$Rss + $Shared + $Private + $Swap + $Pss"|bc -l`
echo "Rss " $Rss
echo "Shared " $Shared
echo "Private " $Private
echo "Swap " $Swap
echo "Pss " $Pss
echo "=================";
echo "Mem " $Mem
echo "=================";
回答by Brian C.
The tool you want is ps. To get information about what java programs are doing:
你要的工具是ps。要获取有关 Java 程序正在执行的操作的信息:
ps -F -C java
To get information about http:
要获取有关 http 的信息:
ps -F -C httpd
If your program is ending before you get a chance to run these, open another terminal and run:
如果您的程序在您有机会运行它们之前就结束了,请打开另一个终端并运行:
while true; do ps -F -C myCoolCode ; sleep 0.5s ; done
回答by Sunil Bojanapally
回答by Peter
First get the pid:
首先获取pid:
ps ax | grep [process name]
And then:
进而:
top -p PID
You can watch various processes in the same time:
您可以同时观看各种流程:
top -p PID1 -p PID2
回答by Ilia Choly
You can use pmap+ awk.
您可以使用pmap+ awk。
Most likely, we're interested in the RSSmemory which is the 3rd column in the last line of the example pmapoutput below (82564).
最有可能的是,我们对RSS内存感兴趣,它是pmap下面示例输出 (82564)最后一行中的第三列。
$ pmap -x <pid>
Address Kbytes RSS Dirty Mode Mapping
....
00007f9caf3e7000 4 4 4 r---- ld-2.17.so
00007f9caf3e8000 8 8 8 rw--- ld-2.17.so
00007fffe8931000 132 12 12 rw--- [ stack ]
00007fffe89fe000 8 8 0 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------ ------ ------
total kB 688584 82564 9592
Awk is then used to extract that value.
然后使用 awk 提取该值。
$ pmap -x <pid> | awk '/total/ { print "K" }'
The pmapvalues are in kilobytes. If we wanted it in megabytes, we could do something like this.
该pmap值以千字节为单位。如果我们想要以兆字节为单位,我们可以做这样的事情。
$ pmap -x <pid> | awk '/total/ { print / 1024 "M" }'
回答by Greg Slepak
I don't know why the answer seem so complicated... It seems pretty simple to do this with ps:
我不知道为什么答案看起来如此复杂......这样做似乎很简单ps:
mem()
{
ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf /1024 "MB"; =""; print }'
}
Example usage:
用法示例:
$ mem mysql
0.511719MB 781 root /bin/sh /usr/bin/mysqld_safe
0.511719MB 1124 root logger -t mysqld -p daemon.error
2.53516MB 1123 mysql /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
回答by Costa
Use
用
- ps u `pidof $TASKS_LIST`or ps u -C $TASK
- ps xu --sort %mem
- ps h -o pmem -C $TASK
- ps u `pidof $TASKS_LIST`或ps u -C $TASK
- ps xu --sort %mem
- ps h -o pmem -C $TASK
Example:
例子:
ps-of()
{
ps u `pidof "$@"`
}
$ ps-of firefox
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 18464 5.9 9.4 1190224 372496 ? Sl 11:28 0:33 /usr/lib/firefox/firefox
$ alias ps-mem="ps xu --sort %mem | sed -e :a -e '1p;$q;N;6,$D;ba'"
$ ps-mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
const 3656 0.0 0.4 565728 18648 ? Sl Nov21 0:56 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
const 11361 0.3 0.5 1054156 20372 ? Sl Nov25 43:50 /usr/bin/python /usr/bin/ubuntuone-control-panel-qt
const 3402 0.0 0.5 1415848 23328 ? Sl Nov21 1:16 nautilus -n
const 3577 2.3 2.0 1534020 79844 ? Sl Nov21 410:02 konsole
const 18464 6.6 12.7 1317832 501580 ? Sl 11:28 1:34 /usr/lib/firefox/firefox
$ ps h -o pmem -C firefox
12.7

