Skip to main content

Module memory_events

Module memory_events 

Source
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 per ChangeKind.
Change
One observed allocation change, copied out of mi_memory_change_t.
Snapshot
Running totals maintained while tracking is enabled.

Enums§

ChangeKind
Which kind of change a Change describes.

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. Returns false if 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 visitor with each one’s address and usable size. Return false from visitor to stop early.