trace_allocs

Function trace_allocs 

Source
pub fn trace_allocs<F: FnOnce() -> O, O>(f: F) -> (O, MemoryStats)
Expand description

Traces allocations performed while executing the f.

Beware that allocations made by nother threads will be also recorded.

use alloc_test::{TracingAllocator, MemoryTracingHooks, trace_allocs};
use std::alloc::System;

#[global_allocator]
static ALLOCATOR: TracingAllocator<MemoryTracingHooks, System> =
    TracingAllocator::new(MemoryTracingHooks, System);

fn main() {
    let (_, stats) = trace_allocs(|| {
        let r: Vec<u8> = vec![1, 2, 3];
        r
    });
    assert_eq!(stats.peak, 3);
    assert_eq!(stats.current, 3);
}