https://github.com/casper-network/datasize-rs
Heap memory usage estimation
https://github.com/casper-network/datasize-rs
Last synced: 5 months ago
JSON representation
Heap memory usage estimation
- Host: GitHub
- URL: https://github.com/casper-network/datasize-rs
- Owner: casper-network
- License: other
- Created: 2020-09-23T20:01:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-10T14:21:51.000Z (over 2 years ago)
- Last Synced: 2025-12-19T21:55:07.976Z (6 months ago)
- Language: Rust
- Size: 194 KB
- Stars: 19
- Watchers: 3
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# data_size::<T>(&T)
The `datasize` crate is used for for estimating the heap memory usage of values, e.g. the number of bytes used by a `Vec` outside its on-stack size determined by `mem::size_of`.
`datasize` is intended for rough benchmarks, typically to find memory hogs (it won't find memory leaks, as memory that is not reachable will not be reported). While it may not give entirely accurate readings in all situations, it will quickly identify low-hanging fruit.
## Example
The `DataSize` trait is implemented for many primitive and `std` types, and can be derived for `structs` and others:
```rust
use datasize::DataSize;
#[derive(DataSize)]
struct Example {
count: usize,
my_data: Vec,
warning: Option>,
#[data_size(skip)]
skipped: Box,
}
```
Any instance `ex` of the `Example` can now report an estimate of its heap allocation through `data_size(&ex)`.
## Other works
Other crates suitable for this task are [`heapsize`](https://docs.rs/heapsize/) and [`malloc_size_of`](https://github.com/servo/servo/tree/faf3a183f3755a9986ec4379abadf3523bd8b3c0/components/malloc_size_of). Unfortunately the `heapsize` crate has been discontinued and the `malloc_size_of` crate puts some rather heavy constraints on what allocator can be used.