| Crates.io | thresher |
| lib.rs | thresher |
| version | 0.1.0 |
| created_at | 2025-08-31 23:50:40.614501+00 |
| updated_at | 2025-08-31 23:50:40.614501+00 |
| description | Execute a callback when a memory threshold is reached |
| homepage | |
| repository | https://github.com/cetra3/thresher |
| max_upload_size | |
| id | 1818970 |
| size | 45,755 |
A memory allocation wrapper that hits a callback when a threshold is reached:
#[global_allocator]
static ALLOCATOR: Thresher<alloc::System> = Thresher::new(alloc::System);
fn main() {
ALLOCATOR.set_threshold(100 * 1024 * 1024);
ALLOCATOR.set_callback(|allocation| {
println!("Threshold reached! Allocated: {} bytes", allocation);
});
}
While there are crates to limit and cap memory usage, there are occasions where you want to know what's going on before ending the process. However, running any sort of diagnostic may require you to allocate more memory, which means you do need a little bit of headroom in order to have this be useful. This is what this library is for: having a threshold of memory usage, after which actions can be taken to either reduce memory or provide enough information to know what's going on.
Here are a few uses:
if you have processes that are being killed by OOM, then you may want to record a heap profile of what's happening. I.e, set the threshold to 90% of available memory, and have it write a heap dump. This is essentially the main motivation for this library.
Another situation may be to provide some back pressure or slow down requests to prevent an OOM in the first place. I.e, if things are happening too quickly.
You could also use this threshold as an opportunity to dump buffers/drop potential memory hogs. I.e, reqwest/hyper have write buffers that are never sized down.
examples/basic.rs example for a bare bones version of this.examples/jemalloc.rs for a way to wire up and have it dump a heap profile.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.