Skip to main content

Crate mod_alloc

Crate mod_alloc 

Source
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):

CounterUpdated on allocUpdated on dealloc
alloc_count+= 1(unchanged)
total_bytes+= size(unchanged)
current_bytes+= size-= size
peak_byteshigh-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.0 ships Tier 1 (counters) only. The backtraces and dhat-compat cargo features are defined for forward compatibility but compile as no-ops; Tier 2 (inline backtrace capture) lands in v0.9.1 and Tier 3 (DHAT-compatible JSON output) lands in v0.9.3.

Structs§

AllocStats
Snapshot of allocation statistics at a point in time.
ModAlloc
Global allocator wrapper that tracks allocations.
Profiler
Scoped profiler that captures a delta between start and stop.