Expand description
A an allocator-agnostic crate for measuring the runtime size of a value including the size of any heap allocations that are owned by that value.
- The core abstraction is provided by the
MallocSizeOftrait which should be implemented for all types whose size you wish to measure. - A derive macro for implementing this trait on structs is provided by the malloc_size_of_derive crate
- Additionally there are
MallocUnconditionalSizeOf,MallocConditionalSizeOftraits for measuring types where ownership is shared (such asArcandRc). - Each of these traits also has a “shallow” variant (
MallocShallowSizeOf,MallocUnconditionalShallowSizeOf, andMallocConditionalShallowSizeOf) which only measure the heap size of the value passed and not any nested allocations.
All of these traits rely on being provided with an instance of MallocSizeOfOps which allows size computations
to call into the allocator to ask it for the underlyinhg size of the allocations backing data structures.
This crate is used by both Servo and Firefox for memory usage calculation.
§Features
- It isn’t bound to a particular heap allocator.
- It provides traits for both “shallow” and “deep” measurement, which gives flexibility in the cases where the traits can’t be used.
- It allows for measuring blocks even when only an interior pointer can be
obtained for heap allocations, e.g.
HashSetandHashMap. (This relies on the heap allocator having suitable support, whichjemallochas.) - It allows handling of types like
RcandArcby providing traits that are different to the ones for non-graph structures.
§Suggested usage
- When possible, use the
MallocSizeOftrait. (Deriving support is provided by themalloc_size_of_derivecrate.) - If you need an additional synchronization argument, provide a function that is like the standard trait method, but with the extra argument.
- If you need multiple measurements for a type, provide a function named
add_size_ofthat takes a mutable reference to a struct that contains the multiple measurement fields. - When deep measurement (via
MallocSizeOf) cannot be implemented for a type, shallow measurement (viaMallocShallowSizeOf) in combination with iteration can be a useful substitute. RcandArcare always tricky, which is whyMallocSizeOfis not (and should not be) implemented for them.- If an
RcorArcis known to be a “primary” reference and can always be measured, it should be measured via theMallocUnconditionalSizeOftrait. - If an
RcorArcshould be measured only if it hasn’t been seen before, it should be measured via theMallocConditionalSizeOftrait. - Using universal function call syntax is a good idea when measuring boxed
fields in structs, because it makes it clear that the Box is being
measured as well as the thing it points to. E.g.
<Box<_> as MallocSizeOf>::size_of(field, ops).
Macros§
- malloc_
size_ of_ is_ 0 - For use on types where size_of() returns 0.
Structs§
- Malloc
Size OfOps - Operations used when measuring heap usage of data structures.
Traits§
- Malloc
Conditional Shallow Size Of MallocConditionalSizeOfcombined withMallocShallowSizeOf.- Malloc
Conditional Size Of - Like
MallocSizeOf, but only measures if the value hasn’t already been measured. For use with types likeRcandArcwhen appropriate (e.g. when there is no “primary” reference). - Malloc
Shallow Size Of - Trait for measuring the “shallow” heap usage of a container.
- Malloc
Size Of - Trait for measuring the “deep” heap usage of a data structure. This is the most commonly-used of the traits.
- Malloc
Unconditional Shallow Size Of MallocUnconditionalSizeOfcombined withMallocShallowSizeOf.- Malloc
Unconditional Size Of - Like
MallocSizeOf, but with a different name so it cannot be used accidentally with derive(MallocSizeOf). For use with types likeRcandArcwhen appropriate (e.g. when measuring a “primary” reference).