Expand description
Opt-in allocation-change accounting and callbacks (include/mimalloc/memory-events.h).
Independent of the sampled profiler: this module is compiled into the C library in
every configuration, including default-features = false. Tracking is off by
default; while it is off every allocate/free/realloc pays for exactly one relaxed flag
check and nothing else.
use mimalloc_pprof::{memory_events, MiMalloc};
// The counters only move for allocations that actually reach mimalloc, so this
// example is only meaningful once mimalloc is the global allocator.
#[global_allocator]
static ALLOCATOR: MiMalloc = MiMalloc;
fn main() {
memory_events::set_enabled(true);
let before = memory_events::snapshot().expect("snapshot").accum_count;
let v = vec![0_u8; 4096];
std::hint::black_box(&v);
let after = memory_events::snapshot().expect("snapshot").accum_count;
assert!(after > before);
memory_events::set_enabled(false);
}Structs§
- Callbacks
- Handlers to install with
set_callbacks, one perChangeKind. - Change
- One observed allocation change, copied out of
mi_memory_change_t. - Snapshot
- Running totals maintained while tracking is enabled.
Enums§
- Change
Kind - Which kind of change a
Changedescribes.
Functions§
- clear_
callbacks - Remove every installed callback. Accounting (and
snapshot) keeps working. - is_
enabled - Whether tracking is on.
- set_
callbacks - Install
callbacks, replacing any previous table. Returnsfalseif the C library refused the table. - set_
enabled - Enable or disable tracking; returns the previous state.
- snapshot
- Read the running totals.
- visit_
live_ ⚠allocations - Walk the live allocations this thread may safely observe, calling
visitorwith each one’s address and usable size. Returnfalsefromvisitorto stop early.