peakmem-alloc

Crates.iopeakmem-alloc
lib.rspeakmem-alloc
version0.3.0
sourcesrc
created_at2023-04-05 05:19:00.743255
updated_at2024-03-18 10:14:51.05976
descriptionAn allocator wrapper that allows measuring peak memory consumption
homepage
repositoryhttps://github.com/PSeitz/peakmem-alloc
max_upload_size
id830687
size9,183
(PSeitz)

documentation

README

peakmem_alloc

An instrumenting middleware for global allocators in Rust, useful to find the peak memory consumed by a function.

Example

use peakmem_alloc::*;
use std::alloc::System;

#[global_allocator]
static GLOBAL: &PeakMemAlloc<System> = &INSTRUMENTED_SYSTEM;

#[test]
fn example_using_region() {
    GLOBAL.reset_peak_memory(); // Note that other threads may impact the peak memory computation.
    let _x: Vec<u8> = Vec::with_capacity(1_024);
    println!(
        "Peak Memory used by function : {:#?}", 
        GLOBAL.get_peak_memory()
    );
}

Custom allocators

You can wrap your existing allocator as follows:

use jemallocator::Jemalloc;
use peakmem_alloc::*;

#[global_allocator]
static GLOBAL: PeakMemAlloc<Jemalloc> = PeakMemAlloc::new(Jemalloc);

Commit count: 11

cargo fmt