linux – 如何测量应用程序或进程的实际内存使用情况?

这个问题是非常详细的here

如何在Linux中测量应用程序或进程的内存使用情况?

Understanding memory usage on Linux的博客文章,ps不是用于此意图的准确工具。

Why ps is “wrong”

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely wrong.

使用ps或类似工具,您将只获得该进程分配的内存页的数量。这个数字是正确的,但是:

>不反映应用程序使用的实际内存量,仅反映为其保留的内存量
>如果页面被共享,例如通过多个线程或通过使用动态链接的库,则可能是误导

如果你真的想知道你的应用程序实际使用的内存量,你需要在分析器中运行它。例如,valgrind可以让您了解所使用的内存量,更重要的是,您的程序可能出现的内存泄漏。 valgrind的heap profiler工具称为“massif”:

Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program’s heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

valgrind documentation中所解释的,您需要通过valgrind运行程序:

valgrind --tool=massif <executable> <arguments>

Massif写入一个内存使用快照转储(例如,massif.out.12345)。这些提供了(1)内存使用的时间线,(2)对于每个快照,分配了程序存储器中的哪个位置的记录。
一个伟大的图形工具,用于分析这些文件是massif-visualizer.但我发现ms_print,一个简单的基于文本的工具与valgrind一起提供,已经是很大的帮助。

要查找内存泄漏,请使用valgrind的(默认)memcheck工具。

http://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:linux – 如何测量应用程序或进程的实际内存使用情况?