#size #stack #heap #macro

macro deepsize_derive

A crate for measuring the total size of object on the stack and heap

3 releases

0.1.2 Nov 13, 2021
0.1.1 Jul 13, 2019
0.1.0 Jan 23, 2019

#144 in #heap

Download history 13174/week @ 2024-03-14 13500/week @ 2024-03-21 13317/week @ 2024-03-28 16545/week @ 2024-04-04 14110/week @ 2024-04-11 14394/week @ 2024-04-18 17504/week @ 2024-04-25 16603/week @ 2024-05-02 16199/week @ 2024-05-09 15110/week @ 2024-05-16 14456/week @ 2024-05-23 20344/week @ 2024-05-30 23457/week @ 2024-06-06 17392/week @ 2024-06-13 18887/week @ 2024-06-20 17774/week @ 2024-06-27

82,370 downloads per month
Used in deepsize

MIT license

8KB
136 lines

deepsize

A trait and derive macro to recursively find the size of an object and the size of allocations that it owns.

This should work in #[no_std] environments, but requires the alloc crate.

Ownership and Reference Counting

DeepSizeOf counts all memory considered "owned" by the structure that it is finding the size of. Structures behind & and &mut references are not counted towards the total size of the structure; however, uniquely owned structures such as Box and Vec are.

Reference counted pointers (Arc, and Rc) are counted the first time that they appear, and are tracked to prevent them from being counted multiple times. The Weak variants of each are treated like references, and are not counted.

Features

  • std (enabled by default): Adds implementations of DeepSizeOf for types only found in std such as HashMap and Mutex.
  • derive (enabled by default): Adds support for a derive macro for DeepSizeOf.

deepsize also has optional support for these external crates:

  • slotmap: (version 0.4)
  • slab: (version 0.4)
  • indexmap: (version 1)
  • arrayvec: (version 0.5)
  • smallvec: (version 1)
  • hashbrown: (version 0.9)
  • chrono: (version 0.4)

Example Code

use std::mem::size_of;

use deepsize::DeepSizeOf;

#[derive(DeepSizeOf)]
struct Test {
    a: u32,
    b: Box<[u8]>,
}

fn main() {
    let object = Test {
        a: 15,
        b: Box::new(*b"Hello, Wold!"),
    };
    
    assert_eq!(object.deep_size_of(), size_of::<Test>() + size_of::<u8>() * 12);
}

Dependencies

~1.5MB
~35K SLoC