Linux 我如何找到哪个进程正在泄漏内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/143791/
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 do i find which process is leaking memory
提问by
I have a system (Ubuntu) with many processes and one (or more) have a memory leak. Is there a good way to find the process that has the leak? Some of the process are JVMs, some are not. Some are home grown some are open source.
我有一个包含多个进程的系统 (Ubuntu),一个(或多个)有内存泄漏。有没有什么好的方法可以找到有泄漏的进程?有些进程是 JVM,有些则不是。有些是自产的,有些是开源的。
回答by unexist
Difficult task. I would normally suggest to grab a debugger/memory profiler like Valgrindand run the programs one after one in it. Soon or later you will find the program that leaks and can tell it the devloper or fix it yourself.
困难的任务。我通常会建议使用像Valgrind这样的调试器/内存分析器,并在其中一个接一个地运行程序。迟早你会发现泄漏的程序,并可以告诉开发人员或自己修复它。
回答by Adam Rosenfield
You can run the top
command (to run non-interactively, type top -b -n 1
). To see applications which are leaking memory, look at the following columns:
您可以运行该top
命令(要以非交互方式运行,请键入top -b -n 1
)。要查看内存泄漏的应用程序,请查看以下列:
- RPRVT - resident private address space size
- RSHRD - resident shared address space size
- RSIZE - resident memory size
- VPRVT - private address space size
- VSIZE - total memory size
- RPRVT - 常驻私有地址空间大小
- RSHRD - 常驻共享地址空间大小
- RSIZE - 常驻内存大小
- VPRVT - 私有地址空间大小
- VSIZE - 总内存大小
回答by Remo.D
As suggeseted, the way to go is valgrind. It's a profiler that checks many aspects of the running performance of your application, including the usage of memory.
正如所建议的那样,要走的路是 valgrind。它是一个分析器,可以检查应用程序运行性能的许多方面,包括内存使用情况。
Running your application through Valgrind will allow you to verify if you forget to release memory allocated with malloc, if you free the same memory twice etc.
通过 Valgrind 运行您的应用程序将允许您验证是否忘记释放使用 malloc 分配的内存,是否释放相同的内存两次等。
回答by dmityugov
In addition to top, you can use System Monitor (System - Administration - System Monitor, then select Processes tab). Select View - All Processes, go to Edit - Preferences and enable Virtual Memory column. Sort either by this column, or by Memory column
除了top,还可以使用系统监视器(系统-管理-系统监视器,然后选择进程选项卡)。选择查看 - 所有进程,转到编辑 - 首选项并启用虚拟内存列。按此列或内存列排序
回答by Dprado
if the program leaks over a long time, top might not be practical. I would write a simple shell scripts that appends the result of "ps aux" to a file every X seconds, depending on how long it takes to leak significant amounts of memory. Something like:
如果程序长时间泄漏,top 可能不实用。我会编写一个简单的 shell 脚本,每 X 秒将“ps aux”的结果附加到一个文件中,具体取决于泄漏大量内存所需的时间。就像是:
while true
do
echo "---------------------------------" >> /tmp/mem_usage
date >> /tmp/mem_usage
ps aux >> /tmp/mem_usage
sleep 60
done
回答by user20493
If you can't do it deductively, consider the Signal Flare debugging pattern: Increase the amount of memory allocated by one process by a factor of ten. Then run your program.
如果你不能用演绎法来做,考虑一下 Signal Flare 调试模式:将一个进程分配的内存量增加 10 倍。然后运行你的程序。
If the amount of the memory leaked is the same, that process was not the source of the leak; restore the process and make the same modification to the next process.
如果泄漏的内存量相同,则该进程不是泄漏源;恢复流程并对下一个流程进行相同的修改。
When you hit the process that is responsible, you'll see the size of your memory leak jump (the "signal flare"). You can narrow it down still further by selectively increasing the allocation size of separate statements within this process.
当您点击负责的进程时,您将看到内存泄漏跳跃的大小(“信号耀斑”)。您可以通过有选择地增加此过程中单独语句的分配大小来进一步缩小范围。
回答by Gianluca Della Vedova
I suggest the use of htop, as a better alternative to top.
我建议使用 htop,作为 top 的更好替代品。