stats_alloc

Crates.iostats_alloc
lib.rsstats_alloc
version0.1.10
sourcesrc
created_at2018-08-03 16:12:05.17752
updated_at2022-03-30 13:36:01.241473
descriptionAn allocator wrapper that allows for instrumenting global allocators
homepage
repositoryhttps://github.com/neoeinstein/stats_alloc
max_upload_size
id77356
size15,846
Marcus Griep (neoeinstein)

documentation

https://docs.rs/stats_alloc/

README

stats_alloc

An instrumenting middleware for global allocators in Rust, useful in testing for validating assumptions regarding allocation patterns, and potentially in production loads to monitor for memory leaks.

Example

extern crate stats_alloc;

use stats_alloc::{StatsAlloc, Region, INSTRUMENTED_SYSTEM};
use std::alloc::System;

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

fn example_using_region() {
    let reg = Region::new(&GLOBAL);
    let x: Vec<u8> = Vec::with_capacity(1_024);
    println!("Stats at 1: {:#?}", reg.change());
    // Used here to ensure that the value is not
    // dropped before we check the statistics
    ::std::mem::size_of_val(&x);
}

Custom allocators

Currenty wrapping a custom allocator requires the use of the nightly compiler and compiling with the "nightly" feature due to the soon to stabilize use of the unstable const_fn_trait_bound and the fact that the internals of the instrumenting type are not public. If that's fine with you, a custom allocator can be wrapped as follows:

#[global_allocator]
static GLOBAL: StatsAlloc<System> = StatsAlloc::new(MyCustomAllocator::new());
Commit count: 33

cargo fmt