8 breaking releases
Uses old Rust 2015
| new 0.9.0 | Nov 12, 2025 |
|---|---|
| 0.7.0 | Sep 5, 2025 |
| 0.5.0 | Jul 3, 2025 |
| 0.2.0 | Mar 31, 2025 |
| 0.1.1 | Oct 12, 2024 |
#254 in Memory management
4,115 downloads per month
Used in 17 crates
(3 directly)
400KB
8K
SLoC
A crate for measuring the heap usage of data structures in a way that integrates with Firefox's memory reporting, particularly the use of mozjemalloc and DMD. In particular, it has the following 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, which mozjemalloc has.) - It allows handling of types like
RcandArcby providing traits that are different to the ones for non-graph structures.
Suggested uses are as follows.
-
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).Note: WebRender has a reduced fork of this crate, so that we can avoid publishing this crate on crates.io.
Dependencies
~1.6–2.3MB
~49K SLoC