一、什么时候用
程序的 RSS(驻留内存)超出预期,想知道谁分配了这么多内存。
# 运行 Massif
valgrind --tool=massif ./my_program
# 输出文件:massif.out.12345
# 用 ms_print 生成报告
ms_print massif.out.12345
# 输出:
# ---------------------------------------------------------------------------
# Command: ./my_program
# Massif output
# ---------------------------------------------------------------------------
# MB
# 3.25 ┤ ╭╮
# 2.99 ┤ ││ ╭──╮
# 2.74 ┤ ││ │ │
# 2.48 ┤ ╭──╮ │╰─╯ ╰──╮
# 2.23 ┤ │ │ │ │
# 1.97 ┤ │ ├───────╯ │
# 1.72 ┤ ╭───╯ │ │
# 1.46 ┤ │ │ │
# 1.21 ┤ │ │ │
# 0.95 ┤──╯ │ │
# ─┴─────────┴────────────────
# 0 7.9 15.8 time(s)
#
# 可以看到在第 8 秒左右有一次大幅的内存分配。
# 看下面的详细信息找谁分配了这些内存:
# --------------------------------------------------------------------------
# n time(B) total(B) useful-heap(B) extra-heap(B) stacks(B)
# --------------------------------------------------------------------------
# 20 7,891,234 3,123,456 3,100,000 23,456 12,345
# 99.2% (3,100,000) 的堆内存分配在 process_frame 中
# 其中 80% 是在 process_frame → allocate_frame_buffer
# 说明大部分内存被帧缓冲占用了