13 releases

0.2.11 Nov 4, 2022
0.2.10 Dec 10, 2021
0.2.9 Feb 19, 2021
0.2.3 Nov 18, 2020
0.1.1 Sep 27, 2020

#360 in Rust patterns

Download history 629/week @ 2022-08-15 702/week @ 2022-08-22 369/week @ 2022-08-29 564/week @ 2022-09-05 616/week @ 2022-09-12 442/week @ 2022-09-19 408/week @ 2022-09-26 530/week @ 2022-10-03 458/week @ 2022-10-10 453/week @ 2022-10-17 456/week @ 2022-10-24 600/week @ 2022-10-31 728/week @ 2022-11-07 714/week @ 2022-11-14 672/week @ 2022-11-21 877/week @ 2022-11-28

3,029 downloads per month
Used in 14 crates (7 directly)

MIT/Apache

39KB
834 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.2–1.5MB
~28K SLoC