Heaptrack

mohitmishra786/low-level-dev-skills/skills/profilers/heaptrack

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

heaptrack memory profiler skill for Linux. Use when tracking heap allocations, finding memory leaks, measuring peak heap usage, identifying allocation hotspots, or comparing allocation behaviour between runs. Activates on queries about heaptrack, heap profiling, memory allocation analysis, heaptrack_print, allocation hotspots, or memory leak detection with heaptrack.

AI 產生的概覽

引導代理在 Linux 上使用 heaptrack 進行堆積分析,找出洩漏、熱點與尖峰記憶體用量。

功能
此技能提供在 Linux 上使用 heaptrack 記憶體分析工具的說明,涵蓋安裝、錄製配置追蹤,以及用 heaptrack_print 或圖形介面進行分析。它說明如何解讀 heaptrack_print 輸出、篩選結果、比較兩次執行,並透過系統配置器分析 Rust 執行檔。它也包含 heaptrack 與 Valgrind massif 的比較,並指向一份分析參考文件。
適用情境
適用於追蹤堆積配置、追查記憶體洩漏、測量堆積尖峰用量、找出配置熱點,或比較不同程式執行之間的配置行為。也適合回答關於 heaptrack、heaptrack_print 輸出或 Linux 堆積分析的問題。
執行需求
需要在 Linux 上安裝 heaptrack,可選安裝 heaptrack-gui 以及用於視覺化的 flamegraph 工具;程式應以除錯符號建置,以取得可讀的呼叫堆疊。此技能不附帶指令碼,僅包含說明與一份參考文件。

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.

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?"

Workflow

1. Installation

bash
# Ubuntu/Debiansudo apt-get install heaptrack heaptrack-gui
# Fedorasudo dnf install heaptrack
# Archsudo pacman -S heaptrack
# Build from sourcegit clone https://github.com/KDE/heaptrack.gitcmake -S heaptrack -B heaptrack-build -DCMAKE_BUILD_TYPE=Releasecmake --build heaptrack-build -j$(nproc)

2. Basic usage

bash
# Profile a program (generates heaptrack.<prog>.<pid>.zst)heaptrack ./myapp arg1 arg2
# Attach to running processheaptrack --pid 12345
# Analyse the trace fileheaptrack_print heaptrack.myapp.12345.zst
# GUI analysis (if heaptrack-gui installed)heaptrack_gui heaptrack.myapp.12345.zst

3. Build for better profiling

bash
# Build with debug symbols (essential for readable backtraces)cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfocmake --build build
# Then profileheaptrack ./build/myapp

4. Interpreting heaptrack_print output

bash
heaptrack_print heaptrack.myapp.*.zst 2>/dev/null

Key sections:

text
total runtime: 2.34scalls to allocation functions: 145,234 (62,064/s)temporary allocations: 89,123 (38,087/s)peak heap memory consumption: 45.23MBpeak RSS (including heap): 78.45MBtotal memory leaked: 2.34MB
# Top allocation hotspots (by peak memory)hotspot 1: 12.34MB peak  myapp::cache::Cache::insert(...)    at src/cache.cpp:142  ...
# Top leaked allocationsleak 1: 2.34MB leaked in 1,234 allocations  myapp::connection::Connection::new(...)    at src/connection.cpp:67
MetricMeaning
total memory leakedMemory allocated but never freed
peak heap consumptionMaximum live heap at any point
temporary allocationsAllocated and freed within one call stack
calls to allocation functionsTotal malloc/new/realloc calls

5. Filtering and analysis options

bash
# Show top N hotspotsheaptrack_print -p 10 heaptrack.myapp.*.zst  # top 10 hotspots
# Show flamegraph dataheaptrack_print -f heaptrack.myapp.*.zst > alloc.foldedflamegraph.pl alloc.folded > alloc.svg
# Show only leaked allocationsheaptrack_print -l heaptrack.myapp.*.zst
# Show allocations above thresholdheaptrack_print --min-cost 1048576 heaptrack.myapp.*.zst  # >1MB only
# Short summaryheaptrack_print -s heaptrack.myapp.*.zst

6. Comparing two runs

bash
# Record baselineheaptrack ./myapp --config baseline.confmv heaptrack.myapp.*.zst before.zst
# Make changes, record againheaptrack ./myapp --config new.confmv heaptrack.myapp.*.zst after.zst
# Compare (shows diff of hotspots)heaptrack_print before.zst | head -20 > before.txtheaptrack_print after.zst  | head -20 > after.txtdiff before.txt after.txt

7. heaptrack vs Valgrind massif

FeatureheaptrackValgrind massif
Overhead~2-3x~20x
OutputCompressed trace + GUIText/ms_print
Leak detectionYesYes
Peak trackingYesYes
Temporal viewYes (GUI)Yes (ms_print)
PlatformLinux onlyLinux, macOS
Needs recompileNoNo
Call graphFull stack tracesFull stack traces

Use heaptrack for most cases; use massif when you need platform portability or detailed snapshot comparison.

8. Integration with Rust

heaptrack works with Rust binaries when using the system allocator:

rust
// Rust: use system allocator so heaptrack can interceptuse std::alloc::System;#[global_allocator]static A: System = System;
bash
# Profile Rust binarycargo build --releaseheaptrack ./target/release/myapp
# Note: debug symbols improve backtracescargo build --profile release-with-debugheaptrack ./target/release-with-debug/myapp

For heaptrack_print output reference and GUI usage, see references/heaptrack-analysis.md [blocked].

Related skills

  • Use skills/profilers/valgrind for Memcheck (correctness) and massif (alternative heap profiler)
  • Use skills/profilers/linux-perf for CPU profiling alongside memory profiling
  • Use skills/rust/rust-profiling for Rust-specific allocation profiling approaches
  • Use skills/runtimes/sanitizers — ASan LeakSanitizer for leak detection without profiling overhead

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/profilers/heaptrack提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架