【问题标题】:How to track lifetime heap usage from repeated allocations如何从重复分配中跟踪生命周期堆使用情况
【发布时间】:2021-06-15 21:04:59
【问题描述】:

我有一个程序在其生命周期内使用的内存总量比我预期的要多得多,我想看看我能做些什么。

我使用 Valgrind 的 memcheck 工具来消除内存泄漏,并使用 Valgrind 的 massif 工具来查看堆快照。 massif 可以告诉我哪些行负责特定时间点的最大堆块。由于地块没有显示任何非常大的东西,我怀疑我的问题是多次执行较小分配的特定行。

如果一些数字会有所帮助:程序运行大约 5 秒,进行密集的数字计算。峰值内存使用量为 1MB。生命周期内存使用量为 10GB。最大的单次分配为 250KB,共进行了 8 次。

因此,我希望看到的不是在任何特定时间点哪些行分配了大量内存,而是在程序的整个生命周期内为每一行分配的总和。我觉得 Valgrind 应该有权访问该信息,因为它正在跟踪每个分配,但我不知道如何让它告诉我。

任何人都可以建议如何使用 Valgrind 来报告此信息,或者另一种工具可以满足我的需求吗?

【问题讨论】:

  • 你使用 Fortran 的POINTER 属性吗?如果是,则停止使用它,除非您正在跟踪目标的所有分配/解除分配。
  • 有趣的是你应该提到这一点。我开始用 Valgrind 研究这个程序,以摆脱所有的指针,我现在已经完成了,但这就是为什么我看到内存使用量有多大。
  • 正如您可能发现的那样,allocate(ptr(10)) 将分配 10 个实体的匿名目标。如果您将 ptr 指针分配给其他对象而没有先解除分配 ptr,则会泄漏内存。

标签: debugging optimization fortran valgrind heap-memory


【解决方案1】:

据我了解,您不想查看当前内存使用情况的“快照”, 但是您想查看堆栈跟踪完成的累积分配,甚至 如果大部分分配的内存被释放。

为此,您可以尝试选项 --xtree-memory=full。

The xtrees produced by the option --xtree-memory or the xtmemory monitor command are showing the following events/resource consumption describing heap usage:

    curB current number of Bytes allocated. The number of allocated bytes is added to the curB value of a stack trace for each allocation. It is decreased when a block allocated by this stack trace is released (by another "freeing" stack trace)

    curBk current number of Blocks allocated, maintained similary to curB : +1 for each allocation, -1 when the block is freed.

    totB total allocated Bytes. This is increased for each allocation with the number of allocated bytes.

    totBk total allocated Blocks, maintained similary to totB : +1 for each allocation.

    totFdB total Freed Bytes, increased each time a block is released by this ("freeing") stack trace : + nr freed bytes for each free operation.

    totFdBk total Freed Blocks, maintained similarly to totFdB : +1 for each free operation.

Note that the last 4 counts are produced only when the --xtree-memory=full was given at startup.

此选项适用于各种工具以及生成的文件 可以可视化 a.o.使用 kcachegrind。

https://www.valgrind.org/docs/manual/manual-core.html#opt.xtree-memoryhttps://www.valgrind.org/docs/manual/manual-core.html#manual-core.xtree 了解更多信息。

您还可以尝试使用 --tool=dhat,这是一个专门用于报告您的程序对分配的内存做了什么的工具。

【讨论】:

  • 与 memcheck 一起使用时效果很好。左边的函数列表表示每个函数的生命周期分配,可以通过'Self'列排序,快速找到最大的罪魁祸首。在每个函数中,右侧的“源代码”选项卡都有每行生命周期分配的逐行列表。
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2010-09-17
相关资源
最近更新 更多