Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

linux - 如何衡量应用程序或进程的实际内存使用情况?(How to measure actual memory usage of an application or process?)

This question is covered here in great detail.

(这个问题在这里详细介绍。)

How do you measure the memory usage of an application or process in Linux?

(您如何衡量Linux中应用程序或进程的内存使用情况?)

From the blog article of Understanding memory usage on Linux , ps is not an accurate tool to use for this intent.

(从了解Linux上的内存使用的博客文章中, ps不是用于此目的的准确工具。)

Why ps is "wrong"

(为什么ps是“错误的”)

Depending on how you look at it, ps is not reporting the real memory usage of processes.

(根据您的看法, ps不会报告进程的实际内存使用情况。)

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 .

(当然,一台典型的Linux计算机在任何给定时间都运行着几十个进程,这意味着ps报告的VSZ和RSS编号几乎肯定是错误的 。)

  ask by ksuralta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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工具。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...