【问题标题】:How can I debug the size of individual container instances in C++?如何在 C++ 中调试单个容器实例的大小?
【发布时间】:2016-06-16 01:37:08
【问题描述】:

我有一个巨大的 C++ 项目,它使用标准容器:vector、map、multimap、set 等。平台是 Linux x86_64,编译器是 g++ 4.9。我遇到了一些大型数据集的内存问题。现在我想知道:有没有办法查看哪些容器的哪些实例占用了最多的内存? 有没有像 valgrind 这样的工具可以做到这一点?可以收集统计信息的修补标准库?

【问题讨论】:

标签: c++ debugging memory-management valgrind


【解决方案1】:

Heaptrack (http://milianw.de/blog/heaptrack-a-heap-memory-profiler-for-linux) 可用于此目的。 Valgrind 中的 Massif 工具也是如此。我已经成功地使用了这两种方法。 您可能还想查看 libstdc++ 的调试模式 - https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html

【讨论】:

    【解决方案2】:

    我发现 valgrind massif 已经非常有用了。我现在这样使用它:

    valgrind --smc-check=all-non-file --tool=massif --max-snapshots=1000 --vgdb=yes --vgdb-error=0 --massif-out-file=/some/dir/massif-%p.out myprogram
    

    这样,我什至可以在启动时获得一个远程 gdb 控制台,并随时转储更多堆快照。启动后,需要附加到gdb并继续执行:

    gdb myprogram
    (gdb) target remote | vgdb --pid=pid_of_myprogram
    (gdb) continue
    

    除了日志文件 massif-[pid].out,您可以随时中断和转储快照:

    ^C
    (gdb) monitor detailed_snapshot mysnapshot-1.out
    (gdb) continue
    

    我需要 smc 检查,因为我的项目包含 JVM。如果您没有自修改代码,则可以将其省略。输出的 1000 个快照可能有点大。默认值为 100。

    【讨论】:

      猜你喜欢
      • 2010-10-26
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多