With ps
or similar tools you will only get the amount of memory pages allocated by that process.
(使用ps
或类似工具,您将只获得该进程分配的内存页面数量。)
This number is correct, but: (该数字是正确的,但是:)
does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it
(不反映应用程序实际使用的内存量,仅反映为其保留的内存量)
can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries
(如果页面被共享(例如由多个线程共享或通过使用动态链接的库)可能会产生误导)
If you really want to know what amount of memory your application actually uses, you need to run it within a profiler.
(如果您确实想知道应用程序实际使用的内存量,则需要在分析器中运行它。)
For example, valgrind
can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. (例如, valgrind
可以为您提供有关已用内存量的信息,更重要的是,您可以了解程序中可能的内存泄漏。)
The heap profiler tool of valgrind is called 'massif': (valgrind的堆分析器工具称为“ massif”:)
Massif is a heap profiler.
(Massif是堆分析器。)
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. (该图由文本或HTML文件补充,该文本或HTML文件包含更多信息,用于确定分配最大内存的位置。)
Massif runs programs about 20x slower than normal. (Massif运行的程序比正常运行慢约20倍。)
As explained in the valgrind documentation , you need to run the program through valgrind:
(如valgrind文档中所述,您需要通过valgrind运行程序:)
valgrind --tool=massif <executable> <arguments>
Massif writes a dump of memory usage snapshots (eg massif.out.12345
).
(Massif写入内存使用情况快照的转储(例如massif.out.12345
)。)
These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. (这些提供了(1)内存使用的时间表,(2)每个快照,记录了程序存储器中的分配位置。)
A great graphical tool for analyzing these files is massif-visualizer . (massif-visualizer是分析这些文件的一种出色的图形工具。)
But I found ms_print
, a simple text-based tool shipped with valgrind, to be of great help already. (但是我发现ms_print
这个由valgrind附带的简单的基于文本的工具已经ms_print
了。)
To find memory leaks, use the (default) memcheck
tool of valgrind.
(要查找内存泄漏,请使用valgrind的(默认) memcheck
工具。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…