| Crates.io | memtally |
| lib.rs | memtally |
| version | 0.1.1 |
| created_at | 2025-06-19 12:32:48.165522+00 |
| updated_at | 2025-06-24 13:03:42.971689+00 |
| description | A wrapper for some collection types that keeps track of indirectly allocated heap memory |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1718307 |
| size | 38,749 |
MemTally provides Tracked<C>, a lightweight wrapper around standard Rust collection types (e.g., HashMap, Vec, HashSet) that tracks the heap-allocated memory used by their elements.
This enables constant-time estimation of memory usage, without the need to again iterate through all elements.
get-size, get-size2, and memuseTo use MemTally, wrap your collection with Tracked<C>. Elements must implement the HeapSize trait.
use memtally::{HeapSize, Tracked};
use std::collections::HashSet;
// Example element type implementing HeapSize
#[derive(Hash, Eq, PartialEq)]
struct Item {
data: String,
}
impl HeapSize for Item {
fn heap_size(&self) -> usize {
self.data.capacity()
}
}
let mut set = Tracked::from(HashSet::new());
set.insert(Item {
data: "hello".into(),
});
println!("Total heap usage: {} bytes", set.heap_size());
All immutable methods from the underlying collection are accessible via Deref. Mutating operations must be performed through Tracked.
To avoid writing manual HeapSize impls for common types, enable one of the following features to use automatic implementations from third-party crates:
get-size: Uses the get-size crateget-size2: Uses the get-size2 cratememuse: Uses the memuse crateEnable them in your Cargo.toml:
memtally = { version = "0.1.0", features = ["get-size"] }
This crate is currently an early prototype. APIs may change, and the accuracy of memory estimation has not been properly tested, and neither have all the mutating methods been verified for correctness. Contributions and feedback are welcome.