gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:13:51 25 4
gpt4 key购买 nike

您如何衡量 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.

(注意:这个问题在 here 中有详细介绍。)

最佳答案

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

  • 不反射(reflect)应用实际使用的内存量,只反射(reflect)为其预留的内存量

  • 如果页面是共享的,例如由多个线程或使用动态链接库共享,可能会产生误导

如果您真的想知道您的应用程序实际使用了多少内存,您需要在分析器中运行它。例如,Valgrind可以让您深入了解已使用的内存量,更重要的是,了解您的程序中可能存在的内存泄漏。 Valgrind 的堆分析器工具称为“massif”:

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

Valgrind documentation 中所述, 需要通过 Valgrind 运行程序:

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

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

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

关于linux - 如何测量应用程序或进程的实际内存使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/131303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com