dhat
Heap profiling, ad hoc profiling, and heap-usage testing for Rust programs, in the style of Valgrind's DHAT.
Repository Health
Technical Analysis
dhat brings the capabilities of Valgrind’s DHAT tool into a Rust crate, letting you profile heap allocations and do ad hoc profiling directly from within your program. You install its global allocator and profiler, run your workload, and get output you can explore in the DHAT viewer to understand where and how your program allocates memory.
Beyond profiling, dhat provides heap-usage testing so you can write assertions like “this code performs exactly 96 allocations”, “peak heap usage stays under 10 MiB”, or “all allocations are freed before finishing”, with helpful diagnostics when they fail. Note that the crate is explicitly experimental and lightly maintained.
What You Get
- Heap profiling that tracks allocation sites, counts, and byte totals
- Ad hoc profiling for counting arbitrary events by call stack
- Output compatible with the online DHAT viewer for interactive exploration
- Heap-usage testing: assert exact allocation counts, peak usage, and full deallocation
- A drop-guard-based Profiler that starts and finalizes profiling around a scope
Common Use Cases
- Finding where a Rust program allocates the most heap memory
- Regression-testing that a hot path performs a bounded number of allocations
- Enforcing a peak-memory ceiling in a test
- Ad hoc counting of events grouped by the call stack that reached them
Under The Hood
Architecture - dhat works by wrapping the global allocator: you set dhat::Alloc as #[global_allocator] and create a dhat::Profiler (heap or ad hoc) whose lifetime scopes the profiling session. Every allocation/deallocation is intercepted, and backtraces are captured to attribute bytes and counts to call stacks. On drop, the Profiler finalizes and can write DHAT-viewer-compatible JSON. For testing, a ProfilerBuilder in testing mode records stats that assertions (allocation count, peak bytes, total blocks) are checked against, with diagnostics on mismatch.
Tech Stack - 100% Rust, distributed as the dhat crate on crates.io with docs on docs.rs. It relies on backtrace capture and global-allocator hooking, which is why the author flags it as depending on fragile implementation techniques across configurations.
Code Quality - The code is authored by Nicholas Nethercote (a Valgrind/DHAT and Rust performance authority) and is widely used (9M+ downloads, ~1k stars). It ships tests, but the README candidly warns the crate is experimental, may crash or hang in some setups, and is not a maintenance priority.
API Design - The API is small and idiomatic: register the allocator, construct a Profiler at the start of main or a test, and let RAII finalize it. Testing helpers read naturally, and the DHAT-viewer integration means users lean on an existing, well-understood UI rather than a bespoke reporting format. The main friction is the global-allocator setup and the experimental caveats.
Used by 2 apps in this directory
agentgateway
AI Development · Developer Tools
An open source AI-native proxy that secures, observes, and governs agent-to-LLM, agent-to-tool, and agent-to-agent communication through MCP, A2A, and unified LLM routing.
Cap
Team Chat · Video Conferencing
Open source Loom alternative with GPU-accelerated recording, instant share links, AI summaries, and full self-hosting via Docker Compose.