Expand description
§mod-alloc
Allocation profiling for Rust. Tracks allocation counts, total
bytes, peak resident memory, and current resident memory by
wrapping the system allocator via GlobalAlloc.
Designed as a lean replacement for dhat with MSRV 1.75 and
zero external dependencies on the hot path.
§Installing as the global allocator
use mod_alloc::{ModAlloc, Profiler};
#[global_allocator]
static GLOBAL: ModAlloc = ModAlloc::new();
fn main() {
let p = Profiler::start();
let v: Vec<u64> = (0..1000).collect();
drop(v);
let stats = p.stop();
println!("Allocations: {}", stats.alloc_count);
println!("Total bytes: {}", stats.total_bytes);
println!("Peak bytes (absolute): {}", stats.peak_bytes);
}§Counter semantics
The four Tier 1 counters track allocator activity since the
installed ModAlloc began counting (or since the last
ModAlloc::reset call):
| Counter | Updated on alloc | Updated on dealloc |
|---|---|---|
alloc_count | += 1 | (unchanged) |
total_bytes | += size | (unchanged) |
current_bytes | += size | -= size |
peak_bytes | high-water mark of current | (unchanged) |
realloc is counted as one allocation event. total_bytes
increases by the growth delta on a growing realloc and is
unchanged on a shrinking realloc.
§Status
v0.9.1 adds Tier 2 (inline backtrace capture) behind the
backtraces feature. Default builds still ship Tier 1
counters only. Tier 3 (DHAT-compatible JSON output) lands in
v0.9.3.
§Backtraces (backtraces feature)
With mod-alloc = { version = "0.9", features = ["backtraces"] }
and RUSTFLAGS="-C force-frame-pointers=yes", each tracked
allocation captures up to 8 frames of its call site via inline
frame-pointer walking on x86_64 and aarch64. Per-call-site
aggregation is exposed via ModAlloc::call_sites (available
only with the backtraces feature); the result is raw return
addresses. Symbolication ships in v0.9.2.
Aggregation-table size is controlled by the MOD_ALLOC_BUCKETS
environment variable at process start (default 4,096 buckets,
~384 KB).
Structs§
- Alloc
Stats - Snapshot of allocation statistics at a point in time.
- Call
Site Stats backtraces - One row in the per-call-site report.
- ModAlloc
- Global allocator wrapper that tracks allocations.
- Profiler
- Scoped profiler that captures a delta between start and stop.