#memory-leaks #memory-allocator #stats #alloc #instrument #statistics

dev stats_alloc

An allocator wrapper that allows for instrumenting global allocators

11 releases

Uses old Rust 2015

0.1.10 Mar 30, 2022
0.1.9 Mar 30, 2022
0.1.8 May 13, 2019
0.1.6 Aug 3, 2018

#33 in Memory management

Download history 165558/week @ 2024-06-10 158725/week @ 2024-06-17 182916/week @ 2024-06-24 168148/week @ 2024-07-01 159939/week @ 2024-07-08 160278/week @ 2024-07-15 162065/week @ 2024-07-22 163204/week @ 2024-07-29 146794/week @ 2024-08-05 160638/week @ 2024-08-12 167890/week @ 2024-08-19 152398/week @ 2024-08-26 136804/week @ 2024-09-02 170466/week @ 2024-09-09 152750/week @ 2024-09-16 174658/week @ 2024-09-23

636,291 downloads per month
Used in 19 crates

MIT license

13KB
188 lines

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

No runtime deps

Features