https://github.com/veeso/leaktracer
A Rust allocator to trace memory allocations in Rust programs, by intercepting the allocations.
https://github.com/veeso/leaktracer
allocator memory-leak memory-tracer rust rust-lang
Last synced: 4 months ago
JSON representation
A Rust allocator to trace memory allocations in Rust programs, by intercepting the allocations.
- Host: GitHub
- URL: https://github.com/veeso/leaktracer
- Owner: veeso
- License: mit
- Created: 2025-06-24T15:04:49.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-07-07T16:44:34.000Z (7 months ago)
- Last Synced: 2025-09-18T04:52:42.528Z (5 months ago)
- Topics: allocator, memory-leak, memory-tracer, rust, rust-lang
- Language: Rust
- Homepage: https://docs.rs/leaktracer
- Size: 38.1 KB
- Stars: 38
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# leaktracer
[](https://opensource.org/licenses/MIT)
[](https://github.com/veeso/leaktracer/stargazers)
[](https://crates.io/crates/leaktracer)
[](https://crates.io/crates/leaktracer)
[](https://ko-fi.com/veeso)
[](https://conventionalcommits.org)
[](https://github.com/veeso/leaktracer/actions)
[](https://coveralls.io/github/veeso/leaktracer)
[](https://docs.rs/leaktracer)
---
- [leaktracer](#leaktracer)
- [Introduction](#introduction)
- [Why do I need this?](#why-do-i-need-this)
- [Usage](#usage)
- [Cargo.toml](#cargotoml)
- [Setup](#setup)
- [Accessing the stats](#accessing-the-stats)
- [Example](#example)
- [Debug only](#debug-only)
- [Support the developer](#support-the-developer)
- [Changelog](#changelog)
- [License](#license)
---
## Introduction
A Rust allocator to trace memory allocations in Rust programs, by intercepting the allocations.
The library provides the `LeaktracerAllocator`, which is an allocator that for each allocation, it stores the memory allocated and the allocation count for each function that allocated memory.
It's extremely easy to setup and it was designed to have something really **plug-and-play**.
### Why do I need this?
You may ask why you would need this library in a language like Rust, which is known for its memory safety. The answer is that even in Rust, memory leaks can occur, especially when storing data in maps or vectors along time without cleaning them up.
Sometimes it can happen that you don't know where the huge memory usage is coming from, because either the cleanup method is not working, or you forgot to clean up the data. In complex applications, this can be a nightmare to debug, so that's why I created this library.
## Usage
### Cargo.toml
Add the following to your `Cargo.toml`:
```toml
[dependencies]
leaktracer = "0.1"
```
### Setup
Then, in your `main.rs` you need to **set the allocator** to `LeaktracerAllocator` and **initialize the symbol table**:
```rust
use leaktracer::LeaktracerAllocator;
#[global_allocator]
static ALLOCATOR: LeaktracerAllocator = LeaktracerAllocator::init();
```
and then at the beginning of your `main` function, initialize the symbol table:
```rust
leaktracer::init_symbol_table(&["my_crate_name"]);
```
The `init_symbol_table` function takes a slice of strings, which are the names of the crates you want to trace. This is useful if you have multiple crates in your project and you want to trace only specific ones.
Why is this necessary? Because the library use the `backtrace` to get the current call stack, but unfortunately the backtrace, is quite *polluted* by other non-relevant calls (such as `std::alloc`, `std::vec`, etc.), so you need to specify which crates you want to trace.
### Accessing the stats
Of course, once initialized you want to access the stats, to see how many allocations were made, and where they were made.
You can do this by accessing the `symbol_table` like this:
```rust
leaktracer::with_symbol_table(|table| {
for (name, symbol) in table.iter() {
println!(
"Symbol: {name}, Allocated: {}, Count: {}",
symbol.allocated(),
symbol.count()
);
}
})?;
```
You can also access the full amount of memory allocated and the total count of allocations by using the `LeaktracerAllocator` methods:
```rust
println!(
"Allocated {} bytes",
ALLOCATOR.allocated()
);
```
## Example
You can find an example in the `examples` folder at `examples/tracing.rs`.
You can run the example with:
```bash
cargo run --example tracing
```
## Debug only
The `LeaktracerAllocator` is meant to be used in debug mode only, as it uses the `backtrace` crate to get the call stack, which is not available in release mode and it's extremely slow and expensive. Therefore, it is not possible to use it in release mode.
## Support the developer
If you like **leaktracer**, please consider a little donation 🥳
[](https://ko-fi.com/veeso)
[](https://www.paypal.me/chrisintin)
---
## Changelog
[View Changelog here](CHANGELOG.md)
---
## License
Licensed under MIT license ([SEE LICENSE](LICENSE) or )