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

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

#295 in Memory management

Download history 117/week @ 2024-11-29 116/week @ 2024-12-06 121/week @ 2024-12-13 85/week @ 2024-12-20 115/week @ 2024-12-27 128/week @ 2025-01-03 201/week @ 2025-01-10 223/week @ 2025-01-17 247/week @ 2025-01-24 230/week @ 2025-01-31 332/week @ 2025-02-07 205/week @ 2025-02-14 295/week @ 2025-02-21 244/week @ 2025-02-28 398/week @ 2025-03-07 290/week @ 2025-03-14

1,268 downloads per month
Used in 6 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