问题描述
此处详细介绍了此问题.
This question is covered here in great detail.
您如何衡量Linux中应用程序或进程的内存使用情况?
How do you measure the memory usage of an application or process in Linux?
来自了解Linux上的内存使用情况的博客文章,ps
并不是用于此目的的准确工具.
From the blog article of Understanding memory usage on Linux, ps
is not an accurate tool to use for this intent.
根据您的看法,ps
不会报告进程的实际内存使用情况.它真正在做的事情是显示每个进程(如果它是唯一运行的进程)会占用多少实际内存.当然,一台典型的Linux计算机在任何给定时间都运行着几十个进程,这意味着ps
报告的VSZ和RSS编号几乎肯定是错误.
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
或类似工具,您将只获得该进程分配的内存页数量.这个数字是正确的,但是:
With ps
or similar tools you will only get the amount of memory pages allocated by that process. 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
可能会产生误导作用
如果您真的想知道您的应用程序实际使用了多少内存,则需要在分析器中运行它.例如,valgrind
可以为您提供有关已用内存量的信息,更重要的是,您可以了解程序中可能的内存泄漏. valgrind的堆分析器工具称为"massif":
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. The heap profiler tool of valgrind is called 'massif':
如 valgrind文档中所述,您需要通过以下方式运行程序valgrind:
As explained in the valgrind documentation, you need to run the program through valgrind:
valgrind --tool=massif <executable> <arguments>
Massif写入内存使用情况快照的转储(例如massif.out.12345
).这些提供(1)内存使用的时间轴,(2)每个快照,记录程序内存中分配位置的记录. massif-visualizer 是一种用于分析这些文件的出色图形工具.但是我发现valgrind附带的一个简单的基于文本的工具ms_print
已经有了很大的帮助.
Massif writes a dump of memory usage snapshots (e.g. 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.A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print
, a simple text-based tool shipped with valgrind, to be of great help already.
要查找内存泄漏,请使用valgrind的(默认)memcheck
工具.
To find memory leaks, use the (default) memcheck
tool of valgrind.
这篇关于如何衡量应用程序或进程的实际内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!