Linux 如何查找进程的内存泄漏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7143776/
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 memory leaks of a process?
提问by nitin_cherian
I need to find out the memory usage of a particular process. In fact I need to find out there is any memory leak in the application I have written. I cannot use memfree
or /proc/meminfo
since our system has log folder mounted in the RAM.
我需要找出特定进程的内存使用情况。事实上,我需要找出我编写的应用程序中是否存在内存泄漏。我无法使用memfree
或/proc/meminfo
因为我们的系统在 RAM 中安装了日志文件夹。
I have gone through lot of similar queries, and some have suggested to use ps aux
command. I'm kinda confused on which parameter gives the correct memory usage or maybe memory leak after few hours. PS AUX
gives VSZ(virtual mem)
and RSS(resident set size)
.
我经历了很多类似的查询,有些人建议使用ps aux
命令。我有点困惑哪个参数给出了正确的内存使用量,或者几个小时后可能出现内存泄漏。PS AUX
给VSZ(virtual mem)
和RSS(resident set size)
.
I have written a sample program which allocates 4 bytes of memory and De-allocates it. After running the program, it seems VSZ
value increases when memory is allocated but not decreased when De-allocated. But RSS
value showed correct, increases when allocated and decreased when De-allocated.
我编写了一个示例程序,它分配了 4 个字节的内存并取消分配了它。运行程序后,似乎VSZ
分配内存时值增加,但释放时不减少。但是RSS
值显示正确,分配时增加,取消分配时减少。
Can anybody confirm whether using RSS
value will point to the amount of memory leak in the code? Or is there any other method?
任何人都可以确认使用RSS
value是否会指向代码中的内存泄漏量?或者还有其他方法吗?
回答by Daniel Pereira
I use top for this sort of thing.
我使用 top 来处理这种事情。
top -p <process id>
回答by GM_ARG
To know the details you can use pmap: pmap pid
要了解详细信息,您可以使用 pmap:pmap pid
root@tm# pmap 1216
1216: /usr/sbin/acpid
08048000 32K r-x-- /usr/sbin/acpid
08050000 4K rw--- /usr/sbin/acpid
08051000 4K rw--- [ anon ]
088f2000 140K rw--- [ anon ]
b7642000 4K rw--- [ anon ]
b7643000 1280K r-x-- /lib/i686/cmov/libc-2.11.3.so
b7783000 4K ----- /lib/i686/cmov/libc-2.11.3.so
b7784000 8K r---- /lib/i686/cmov/libc-2.11.3.so
b7786000 4K rw--- /lib/i686/cmov/libc-2.11.3.so
b7787000 12K rw--- [ anon ]
b7798000 8K rw--- [ anon ]
b779a000 4K r-x-- [ anon ]
b779b000 108K r-x-- /lib/ld-2.11.3.so
b77b6000 4K r---- /lib/ld-2.11.3.so
b77b7000 4K rw--- /lib/ld-2.11.3.so
bfd59000 84K rw--- [ stack ]
total 1704K
回答by Dan Fego
I know this is ancient, but I feel the need to say that for something like this, you really just want to use a tool like Valgrind. Namely, Valgrind. That's definitely the way to go, especially with a program you're writing (or have written), since you can tweak the compile flags as well so you can get the most useful output. Assuming you're using gcc
, try compiling with -g
to turn on debug symbols and don't strip
the binary.
我知道这是古老的,但我觉得有必要说,对于这样的事情,你真的只想使用像Valgrind这样的工具。即,瓦尔格林德。这绝对是要走的路,尤其是对于您正在编写(或已经编写)的程序,因为您还可以调整编译标志,以便获得最有用的输出。假设您正在使用gcc
,请尝试编译-g
以打开调试符号而不是strip
二进制文件。
Usage is pretty simple, and the docs are on the linked website. Basic usage is just valgrind program
on the command line. It'll show you not only specifics, but a nice summary of memory leaked at the end.
用法非常简单,文档在链接的网站上。基本用法只是valgrind program
在命令行上。它不仅会向您展示细节,还会向您展示最后泄漏的内存的一个很好的摘要。