如何测量应用程序或进程的实际内存使用情况? [英] How can I measure the actual memory usage of an application or process?

查看:63
本文介绍了如何测量应用程序或进程的实际内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处详细介绍了此问题.

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 是错误的"

Why ps is "wrong"

根据您的看法, 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':

Massif 是一个堆分析器.它通过对程序堆进行定期快照来执行详细的堆分析.它生成一个图表,显示随时间推移的堆使用情况,包括有关程序的哪些部分负责最多内存分配的信息.该图由文本或HTML文件补充,该文本或HTML文件包含更多信息,用于确定分配最大内存的位置.Massif运行的程序比正常运行慢约20倍.

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文档中所述,您需要通过以下方式运行程序瓦尔格隆德:

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 是一种用于分析这些文件的出色图形工具.但是我发现 ms_print (Valgrind附带的一个简单的基于文本的工具)已经有了很大的帮助.

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.

这篇关于如何测量应用程序或进程的实际内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆