Expand description
§memoria
A bad memory “profiler” for production.
- Define a
UseCase
enum. - Use memoria’s custom allocator.
- Assign usecases to functions or blocks of code using
Alloc::with_usecase
. - Get basic memory usage statistics per-usecase, either at the end of your program or periodically in a background thread.
use num_enum::{TryFromPrimitive, IntoPrimitive};
#[derive(TryFromPrimitive, IntoPrimitive, Default, Debug)]
#[repr(u32)]
enum MyUseCase {
#[default]
None,
LoadConfig,
ProcessData,
}
impl memoria::UseCase for MyUseCase {}
#[global_allocator]
static ALLOCATOR: memoria::Alloc<MyUseCase> = memoria::Alloc::new();
fn load_config() {
let _guard = ALLOCATOR.with_usecase(MyUseCase::LoadConfig);
println!("loading config...");
// consume some memory
let _temporary = vec![0u8; 256];
}
fn process_data() {
let _guard = ALLOCATOR.with_usecase(MyUseCase::ProcessData);
// consume some more memory
let _temporary = vec![0u8; 2048];
}
fn main() {
load_config();
process_data();
println!("memory usage stats:");
ALLOCATOR.with_recorder(|recorder| {
recorder.flush(
|usecase, stat| {
println!("{usecase:?}: {stat:?}");
},
|err, count| {
println!("{err:?}: {count}");
}
);
Ok(())
}).ok();
}
§License
Licensed under the MIT license, see ./LICENSE
.
Structs§
- Alloc
- A wrapper around another allocator
A
that records memory usage statistics intoR
. - Guard
- A drop-guard for setting and resetting the current usecase.
- Stat
- Basic memory stats for a given usecase.
- Stats
Recorder - A simple recorder for memory statistics that can be flushed periodically.
Enums§
- Error
- an error encountered by memoria that caused it to drop stats, such as a detected deadlock that caused it to drop metrics.
Traits§
- Recorder
- A recorder is a structure collecting statistics about memory usage. You might also call it a “metrics sink”.
- UseCase
- A
UseCase
is a struct describing what the application is currently doing. Memory statistics are recorded per distinct value ofUseCase
.
Type Aliases§
- UseCase
Bytes - The internal representation memoria uses to represent instances of
UseCase
.