11 releases

0.2.9 Feb 19, 2021
0.2.8 Feb 18, 2021
0.2.3 Nov 18, 2020
0.2.2 Oct 21, 2020
0.1.1 Sep 27, 2020
Download history 100/week @ 2020-11-10 217/week @ 2020-11-17 405/week @ 2020-11-24 666/week @ 2020-12-01 738/week @ 2020-12-08 17/week @ 2020-12-15 91/week @ 2020-12-22 93/week @ 2020-12-29 43/week @ 2021-01-05 48/week @ 2021-01-12 41/week @ 2021-01-19 22/week @ 2021-01-26 873/week @ 2021-02-02 445/week @ 2021-02-09 680/week @ 2021-02-16 1725/week @ 2021-02-23

1,264 downloads per month
Used in 7 crates (4 directly)

MIT/Apache

39KB
818 lines

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:

use datasize::DataSize;

#[derive(DataSize)]
struct Example {
    count: usize,
    my_data: Vec<MyStruct>,
    warning: Option<Box<u32>>,
    #[data_size(skip)]
    skipped: Box<char>,
}

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 and 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.

Dependencies

~0.4–1.4MB
~31K SLoC