debugging - 如何跟踪重复分配的生命周期堆使用情况

标签 debugging optimization fortran valgrind heap-memory

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

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

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

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

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

最佳答案

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

为此,您可以尝试选项 --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,这是一种专门用于报告您的程序对分配的内存执行了哪些操作的工具。

关于debugging - 如何跟踪重复分配的生命周期堆使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66691508/

相关文章:

python - 如何本地附加到 python 进程?

debugging - 为什么GDB在寄存器信息期间用5填充内存地址的0?

R:使用ROI包

arrays - 最近的 Fortran 标准中提供了此功能吗?

fortran - omp simd的多维数组对齐

c# - 有没有办法在 C# 中的运行时检查堆栈变量?

debugging - 什么可以帮助您提高发现错误的能力?

python - 如何自动触发Python代码?

php - 我应该如何优化/构建我的数据库来收集网上商店的价格发展?

python-2.7 - 如何将fortran 77未格式化的二进制文件读入python