#memory-allocator #wrap #set #memory-tracking #global-allocator #limit-memory

cap

An allocator that can track and limit memory usage. This crate provides a generic allocator that wraps another allocator, tracking memory usage and enabling limits to be set

3 releases

0.1.2 Mar 26, 2023
0.1.1 Aug 15, 2022
0.1.0 Oct 21, 2019

#199 in Memory management

Download history 2895/week @ 2025-10-07 3013/week @ 2025-10-14 3108/week @ 2025-10-21 3534/week @ 2025-10-28 3053/week @ 2025-11-04 2883/week @ 2025-11-11 4187/week @ 2025-11-18 3870/week @ 2025-11-25 4623/week @ 2025-12-02 3919/week @ 2025-12-09 3398/week @ 2025-12-16 2091/week @ 2025-12-23 3433/week @ 2025-12-30 4791/week @ 2026-01-06 4846/week @ 2026-01-13 4880/week @ 2026-01-20

18,307 downloads per month
Used in 11 crates

MIT/Apache

17KB
413 lines

cap

Crates.io MIT / Apache 2.0 licensed Build Status

Docs

An allocator that can track and limit memory usage.

This crate provides a generic allocator that wraps another allocator, tracking memory usage and enabling limits to be set.

Example

It can be used by declaring a static and marking it with the #[global_allocator] attribute:

use std::alloc;
use cap::Cap;

#[global_allocator]
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::max_value());

fn main() {
    // Set the limit to 30MiB.
    ALLOCATOR.set_limit(30 * 1024 * 1024).unwrap();
    // ...
    println!("Currently allocated: {}B", ALLOCATOR.allocated());
}

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

Features