Skip to content

heaptrack - Heap Memory Profiler for Linux Cheatsheet

heaptrack - Heap Memory Profiler for Linux Cheatsheet

heaptrack (by KDE) is a heap memory profiler for Linux. It traces all memory allocations and annotates each with a stack trace, so you can find leaks, allocation hotspots, excessive temporary allocations, and peak memory usage. It records a data file while your program runs, then analyzes it either with the heaptrack_gui (rich flame graphs and charts) or heaptrack_print (command-line). Overhead is low enough for realistic workloads.

Installation

PlatformCommand
Debian/Ubuntusudo apt install heaptrack heaptrack-gui
Fedorasudo dnf install heaptrack heaptrack-gui
Arch Linuxsudo pacman -S heaptrack heaptrack-qt
From sourcebuild from the KDE/heaptrack repo (CMake)
Verifyheaptrack --version

Recording

CommandDescription
heaptrack ./my-program argsProfile a command from the start
heaptrack -p PIDAttach to a running process (GDB-based)
heaptrack -o out.zst ./progWrite to a specific output file
OutputCreates heaptrack.PROG.PID.zst in the current dir

At exit, heaptrack prints a summary and the path to the recorded data file.

Analyzing (GUI)

heaptrack_gui heaptrack.my-program.12345.zst
GUI viewShows
SummaryPeak RSS, total allocations, leaked bytes
Flame graphAllocations attributed to call stacks
Top-down / bottom-upCall trees by allocation
Caller/calleeNavigate allocation relationships
ChartsAllocations/leaks/temporary over time

Analyzing (CLI)

# Print the top allocation hotspots to the terminal
heaptrack_print heaptrack.my-program.12345.zst | less
MetricMeaning
peakMaximum heap memory held at once
leakedMemory allocated but never freed
allocationsTotal number of allocations (churn)
temporaryAllocations freed almost immediately (waste)
OptionDescription
-a, --print-peakSort by peak memory consumers
-l, --print-leaksShow leaks
-t, --print-temporaryShow temporary-allocation hotspots
-d, --diff FILEDiff two recordings

Comparing Runs (Diff)

# See what changed between two builds/runs
heaptrack_print -d before.zst after.zst

What Each Metric Tells You

SymptomLook at
Process RSS keeps growingleaked — allocations never freed
High allocator CPU costallocations / temporary — too much churn
OOM under loadpeak — reduce max simultaneous memory
Startup memory spikeflame graph at the relevant time range

Common Workflows

# Find a leak in a service
heaptrack ./myservice --run-workload
heaptrack_print -l heaptrack.myservice.*.zst | head -30

# Attribute peak memory during a heavy operation (GUI is easiest)
heaptrack ./batch-job
heaptrack_gui heaptrack.batch-job.*.zst   # inspect the "peak" flame graph

# Reduce temporary-allocation churn in a hot loop
heaptrack_print -t heaptrack.*.zst | head

heaptrack vs Other Memory Tools

AspectheaptrackValgrind (massif/memcheck)Built-in allocators
OverheadLowHigh (memcheck very high)Varies
Leak detectionYesYes (memcheck)Partial
Allocation churnYes (temporary)massif focuses on peakNo
UIRich GUI + CLIText / massif-visualizerNone
Best forFast heap profiling on realistic runsDeep correctness checksQuick checks

Resources