| Crates.io | rallo |
| lib.rs | rallo |
| version | 0.4.0 |
| created_at | 2025-03-21 16:46:48.68387+00 |
| updated_at | 2025-06-03 08:21:53.49628+00 |
| description | Rust allocator for tracking memory usage |
| homepage | https://crates.io/crates/rallo |
| repository | https://github.com/oramasearch/rallo |
| max_upload_size | |
| id | 1600808 |
| size | 398,665 |
This crate provides a custom allocator for Rust, useful to track where memories are allocated. You can use it to find where a function or method allocates memory, and how much memory is allocated. At the end, you can create a flamegraph like html page to visualize the memory allocation.
To use this crate, add the following to your Cargo.toml:
[dev-dependencies]
rallo = "*"
Then, create a new file in your tests directory, for example tests/rallo.rs, and add the following code:
use rallo::RalloAllocator;
// This is the maximum length of a frame
const MAX_FRAME_LENGTH: usize = 128;
// Maximum number of allocations to keep
const MAX_LOG_COUNT: usize = 1_024 * 10;
#[global_allocator]
static ALLOCATOR: RalloAllocator<MAX_FRAME_LENGTH, MAX_LOG_COUNT> = RalloAllocator::new();
fn foo() {
let _ = String::with_capacity(1024);
}
#[test]
fn test_rallo() {
ALLOCATOR.start_track();
foo();
ALLOCATOR.stop_track();
// Safety: it is called after `stop_track`
let stats = unsafe { ALLOCATOR.calculate_stats() };
let tree = stats.into_tree().unwrap();
let file_name = "simple-memory-flamegraph.html";
let path = std::env::current_dir().unwrap().join(file_name);
tree.print_flamegraph(&path);
println!("Flamegraph saved to {}", path.display());
}
The generated HTML file will be like this:
