Skip to content

DHAT - Dynamic Heap Analysis Tool Cheatsheet

DHAT - Dynamic Heap Analysis Tool Cheatsheet

DHAT (Dynamic Heap Analysis Tool) is a Valgrind tool that answers a different question from most memory profilers. Rather than only “how much memory did you allocate,” it reports how that memory was actually used: blocks that were allocated but never read, blocks written once and never touched again, very short-lived allocations, and allocations far larger than the bytes actually accessed. That usage view often reveals waste that size-only profilers cannot see.

Installation

PlatformCommand
Debian/Ubuntusudo apt install valgrind
Fedora/RHELsudo dnf install valgrind
Arch Linuxsudo pacman -S valgrind
macOSlimited support; prefer Linux
Verifyvalgrind --tool=dhat --version

Build your program with -g for useful stack traces. DHAT runs under Valgrind, so expect a significant slowdown — this is an analysis tool, not a production profiler.

Running DHAT

valgrind --tool=dhat ./my-program
# → produces dhat.out.<pid>
OptionPurpose
--dhat-out-file=FILEOutput path
--mode=heapDefault heap profiling
--mode=copyProfile memory copying (memcpy etc.)
--mode=ad-hocCustom instrumentation points
--num-callers=NStack depth in reports

Viewing Results

DHAT output is JSON, viewed in the bundled HTML viewer:

# Open the viewer and load dhat.out.<pid>
firefox /usr/libexec/valgrind/dh_view.html
UI elementShows
Tree of allocation pointsGrouped by call stack
Total / max bytesVolume per site
Reads / writesHow much the memory was actually accessed
LifetimesHow long blocks lived
SortingBy any metric to find the worst offenders

The Metrics That Matter

MetricReveals
Total bytesOverall allocation volume from this site
Max bytesPeak simultaneous usage
Reads / writesAccess intensity
Reads = 0Allocated but never read — pure waste
Writes = 0Never initialized — likely a bug or waste
Short lifetimesChurn; candidates for stack/pooling
Access ratioBytes accessed vs bytes allocated

The “reads = 0” case is the classic DHAT find: memory a program dutifully allocates, fills, and frees without anything ever reading it.

Common Findings and Fixes

FindingLikely fix
Many tiny short-lived allocationsStack allocation, arena, or object pool
Large block, tiny accessed portionAllocate only what you use; lazy load
Never-read allocationsDelete the work entirely
Repeated alloc/free of same sizeReuse a buffer
Oversized containersReserve accurate capacity

Copy Mode

valgrind --tool=dhat --mode=copy ./my-program

Copy mode profiles memcpy/memmove volume by call site — useful when profiling shows time in copying and you need to know which code is responsible.

Ad-Hoc Mode

Ad-hoc mode lets you count arbitrary events by annotating your code with DHAT client requests, turning DHAT into a general “which call stack does this the most” counter.

AspectDHATmassifheaptrackBytehound
FocusHow memory is usedPeak usage over timeAllocation profilingInteractive exploration
OverheadHigh (Valgrind)High (Valgrind)LowLow
Unique insightUnread/underused blocksPeak snapshotsChurn + leaksRich filtering
Best forFinding wasted workPeak reductionFast realistic runsDeep interactive analysis

Use heaptrack or Bytehound for low-overhead profiling of realistic runs; reach for DHAT when you want to know whether allocated memory was ever actually used.

Resources