heaptrack
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseheaptrack
heaptrack
Purpose
用途
Guide agents through heaptrack for heap allocation profiling on Linux: recording allocation traces, analysing with heaptrack_print, identifying leaks and hotspots, and comparing runs.
指导Agent使用heaptrack在Linux上进行堆内存分配分析:包括记录分配轨迹、使用heaptrack_print分析数据、识别泄漏与热点,以及对比不同运行实例。
Triggers
触发场景
- "How do I find memory allocation hotspots in my C++ program?"
- "My program uses too much memory — how do I find where?"
- "How do I use heaptrack to detect memory leaks?"
- "What is heaptrack and how does it differ from Valgrind massif?"
- "How do I compare memory usage between two program versions?"
- "heaptrack_print output — how do I interpret it?"
- "如何在我的C++程序中查找内存分配热点?"
- "我的程序内存占用过高——如何定位问题?"
- "如何使用heaptrack检测内存泄漏?"
- "什么是heaptrack,它与Valgrind massif有何区别?"
- "如何对比两个程序版本的内存使用情况?"
- "heaptrack_print的输出——如何解读?"
Workflow
工作流程
1. Installation
1. 安装
bash
undefinedbash
undefinedUbuntu/Debian
Ubuntu/Debian
sudo apt-get install heaptrack heaptrack-gui
sudo apt-get install heaptrack heaptrack-gui
Fedora
Fedora
sudo dnf install heaptrack
sudo dnf install heaptrack
Arch
Arch
sudo pacman -S heaptrack
sudo pacman -S heaptrack
Build from source
从源码构建
git clone https://github.com/KDE/heaptrack.git
cmake -S heaptrack -B heaptrack-build -DCMAKE_BUILD_TYPE=Release
cmake --build heaptrack-build -j$(nproc)
undefinedgit clone https://github.com/KDE/heaptrack.git
cmake -S heaptrack -B heaptrack-build -DCMAKE_BUILD_TYPE=Release
cmake --build heaptrack-build -j$(nproc)
undefined2. Basic usage
2. 基础使用
bash
undefinedbash
undefinedProfile a program (generates heaptrack.<prog>.<pid>.zst)
分析程序(生成heaptrack.<prog>.<pid>.zst文件)
heaptrack ./myapp arg1 arg2
heaptrack ./myapp arg1 arg2
Attach to running process
附加到运行中的进程
heaptrack --pid 12345
heaptrack --pid 12345
Analyse the trace file
分析轨迹文件
heaptrack_print heaptrack.myapp.12345.zst
heaptrack_print heaptrack.myapp.12345.zst
GUI analysis (if heaptrack-gui installed)
GUI分析(需安装heaptrack-gui)
heaptrack_gui heaptrack.myapp.12345.zst
undefinedheaptrack_gui heaptrack.myapp.12345.zst
undefined3. Build for better profiling
3. 编译以优化分析效果
bash
undefinedbash
undefinedBuild with debug symbols (essential for readable backtraces)
带调试符号编译(获取可读回溯信息的关键)
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build
Then profile
然后进行分析
heaptrack ./build/myapp
undefinedheaptrack ./build/myapp
undefined4. Interpreting heaptrack_print output
4. 解读heaptrack_print输出
bash
heaptrack_print heaptrack.myapp.*.zst 2>/dev/nullKey sections:
text
total runtime: 2.34s
calls to allocation functions: 145,234 (62,064/s)
temporary allocations: 89,123 (38,087/s)
peak heap memory consumption: 45.23MB
peak RSS (including heap): 78.45MB
total memory leaked: 2.34MBbash
heaptrack_print heaptrack.myapp.*.zst 2>/dev/null关键部分:
text
total runtime: 2.34s
calls to allocation functions: 145,234 (62,064/s)
temporary allocations: 89,123 (38,087/s)
peak heap memory consumption: 45.23MB
peak RSS (including heap): 78.45MB
total memory leaked: 2.34MBTop allocation hotspots (by peak memory)
Top allocation hotspots (by peak memory)
hotspot 1: 12.34MB peak
myapp::cache::Cache::insert(...)
at src/cache.cpp:142
...
hotspot 1: 12.34MB peak
myapp::cache::Cache::insert(...)
at src/cache.cpp:142
...
Top leaked allocations
Top leaked allocations
leak 1: 2.34MB leaked in 1,234 allocations
myapp::connection::Connection::new(...)
at src/connection.cpp:67
| Metric | Meaning |
|--------|---------|
| `total memory leaked` | Memory allocated but never freed |
| `peak heap consumption` | Maximum live heap at any point |
| `temporary allocations` | Allocated and freed within one call stack |
| `calls to allocation functions` | Total malloc/new/realloc calls |leak 1: 2.34MB leaked in 1,234 allocations
myapp::connection::Connection::new(...)
at src/connection.cpp:67
| 指标 | 含义 |
|--------|---------|
| `total memory leaked` | 已分配但从未释放的内存 |
| `peak heap consumption` | 任意时刻的最大活跃堆内存 |
| `temporary allocations` | 在单个调用栈内分配并释放的内存 |
| `calls to allocation functions` | malloc/new/realloc调用总次数 |5. Filtering and analysis options
5. 过滤与分析选项
bash
undefinedbash
undefinedShow top N hotspots
显示前N个热点
heaptrack_print -p 10 heaptrack.myapp.*.zst # top 10 hotspots
heaptrack_print -p 10 heaptrack.myapp.*.zst # 前10个热点
Show flamegraph data
生成火焰图数据
heaptrack_print -f heaptrack.myapp.*.zst > alloc.folded
flamegraph.pl alloc.folded > alloc.svg
heaptrack_print -f heaptrack.myapp.*.zst > alloc.folded
flamegraph.pl alloc.folded > alloc.svg
Show only leaked allocations
仅显示泄漏的分配
heaptrack_print -l heaptrack.myapp.*.zst
heaptrack_print -l heaptrack.myapp.*.zst
Show allocations above threshold
显示超过阈值的分配
heaptrack_print --min-cost 1048576 heaptrack.myapp.*.zst # >1MB only
heaptrack_print --min-cost 1048576 heaptrack.myapp.*.zst # 仅显示大于1MB的分配
Short summary
简短摘要
heaptrack_print -s heaptrack.myapp.*.zst
undefinedheaptrack_print -s heaptrack.myapp.*.zst
undefined6. Comparing two runs
6. 对比两次运行结果
bash
undefinedbash
undefinedRecord baseline
记录基准数据
heaptrack ./myapp --config baseline.conf
mv heaptrack.myapp.*.zst before.zst
heaptrack ./myapp --config baseline.conf
mv heaptrack.myapp.*.zst before.zst
Make changes, record again
进行修改后,再次记录
heaptrack ./myapp --config new.conf
mv heaptrack.myapp.*.zst after.zst
heaptrack ./myapp --config new.conf
mv heaptrack.myapp.*.zst after.zst
Compare (shows diff of hotspots)
对比(显示热点差异)
heaptrack_print before.zst | head -20 > before.txt
heaptrack_print after.zst | head -20 > after.txt
diff before.txt after.txt
undefinedheaptrack_print before.zst | head -20 > before.txt
heaptrack_print after.zst | head -20 > after.txt
diff before.txt after.txt
undefined7. heaptrack vs Valgrind massif
7. heaptrack与Valgrind massif对比
| Feature | heaptrack | Valgrind massif |
|---|---|---|
| Overhead | ~2-3x | ~20x |
| Output | Compressed trace + GUI | Text/ms_print |
| Leak detection | Yes | Yes |
| Peak tracking | Yes | Yes |
| Temporal view | Yes (GUI) | Yes (ms_print) |
| Platform | Linux only | Linux, macOS |
| Needs recompile | No | No |
| Call graph | Full stack traces | Full stack traces |
Use heaptrack for most cases; use massif when you need platform portability or detailed snapshot comparison.
| 特性 | heaptrack | Valgrind massif |
|---|---|---|
| 性能开销 | ~2-3倍 | ~20倍 |
| 输出形式 | 压缩轨迹文件 + GUI | 文本/ms_print |
| 泄漏检测 | 支持 | 支持 |
| 峰值跟踪 | 支持 | 支持 |
| 时间维度视图 | 支持(GUI) | 支持(ms_print) |
| 支持平台 | 仅Linux | Linux、macOS |
| 是否需要重编译 | 否 | 否 |
| 调用图 | 完整调用栈 | 完整调用栈 |
大多数场景下使用heaptrack;当需要跨平台兼容性或详细快照对比时,使用massif。
8. Integration with Rust
8. 与Rust的集成
heaptrack works with Rust binaries when using the system allocator:
rust
// Rust: use system allocator so heaptrack can intercept
use std::alloc::System;
#[global_allocator]
static A: System = System;bash
undefined当使用系统分配器时,heaptrack可用于Rust二进制文件:
rust
// Rust: 使用系统分配器以便heaptrack可以拦截
use std::alloc::System;
#[global_allocator]
static A: System = System;bash
undefinedProfile Rust binary
分析Rust二进制文件
cargo build --release
heaptrack ./target/release/myapp
cargo build --release
heaptrack ./target/release/myapp
Note: debug symbols improve backtraces
注意:调试符号可提升回溯信息质量
cargo build --profile release-with-debug
heaptrack ./target/release-with-debug/myapp
For heaptrack_print output reference and GUI usage, see [references/heaptrack-analysis.md](references/heaptrack-analysis.md).cargo build --profile release-with-debug
heaptrack ./target/release-with-debug/myapp
关于heaptrack_print输出参考和GUI使用方法,可查看[references/heaptrack-analysis.md](references/heaptrack-analysis.md)。Related skills
相关技能
- Use for Memcheck (correctness) and massif (alternative heap profiler)
skills/profilers/valgrind - Use for CPU profiling alongside memory profiling
skills/profilers/linux-perf - Use for Rust-specific allocation profiling approaches
skills/rust/rust-profiling - Use — ASan LeakSanitizer for leak detection without profiling overhead
skills/runtimes/sanitizers
- 使用进行Memcheck(正确性检查)和massif(替代堆分析工具)分析
skills/profilers/valgrind - 使用搭配内存分析进行CPU分析
skills/profilers/linux-perf - 使用进行Rust专属的分配分析
skills/rust/rust-profiling - 使用—— ASan LeakSanitizer可在无分析开销的情况下检测泄漏
skills/runtimes/sanitizers