17 releases

0.2.15 Sep 11, 2023
0.2.14 Mar 8, 2023
0.2.13 Dec 27, 2022
0.2.11 Nov 4, 2022
0.2.3 Nov 18, 2020

#21 in #accurate

Download history 3537/week @ 2024-09-01 3248/week @ 2024-09-08 4374/week @ 2024-09-15 4343/week @ 2024-09-22 4252/week @ 2024-09-29 5316/week @ 2024-10-06 6731/week @ 2024-10-13 4469/week @ 2024-10-20 5592/week @ 2024-10-27 5217/week @ 2024-11-03 4630/week @ 2024-11-10 5014/week @ 2024-11-17 3907/week @ 2024-11-24 3871/week @ 2024-12-01 3842/week @ 2024-12-08 3932/week @ 2024-12-15

15,900 downloads per month
Used in 33 crates (via datasize)

MIT/Apache

21KB
414 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.


lib.rs:

Proc-macro DataSize derive for use with the datasize crate.

Dependencies

~1.5MB
~37K SLoC