#memory #peak #memory-allocator #alloc #mem #instrument

peakmem-alloc

An allocator wrapper that allows measuring peak memory consumption

5 unstable releases

0.3.0 Mar 18, 2024
0.2.2 Mar 18, 2024
0.2.1 Mar 18, 2024
0.2.0 Mar 18, 2024
0.1.0 Apr 5, 2023

#256 in Memory management

Download history 25/week @ 2024-07-22 53/week @ 2024-07-29 52/week @ 2024-08-05 52/week @ 2024-08-12 16/week @ 2024-08-19 36/week @ 2024-08-26 244/week @ 2024-09-02 93/week @ 2024-09-09 138/week @ 2024-09-16 185/week @ 2024-09-23 71/week @ 2024-09-30 113/week @ 2024-10-07 440/week @ 2024-10-14 375/week @ 2024-10-21 176/week @ 2024-10-28 194/week @ 2024-11-04

1,199 downloads per month
Used in 2 crates (via binggan)

MIT license

8KB
120 lines

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);


lib.rs:

An instrumenting allocator wrapper to compute (scoped) peak memory consumption.

Example

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

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

fn main() {
   GLOBAL.reset_peak_memory();
   let _x: Vec<u8> = Vec::with_capacity(1_024);
   println!(
       "Peak Memory used by function : {:#?}",
       GLOBAL.get_peak_memory()
   );
}

No runtime deps