kpozin
kpozin

Reputation: 26939

Find total size of struct at runtime

Is there any way to calculate the total stack and heap size of a struct at runtime?

As far as I can tell, std::mem::{size_of, size_of_val} only work for stack-allocated values, but a struct might contain heap-allocated buffers, too (e.g. Vec).

Upvotes: 5

Views: 1166

Answers (1)

Akiner Alkan
Akiner Alkan

Reputation: 6872

Servo was using the heapsize crate to measure the size of heap allocations during program.

You can call the heap_size_of function to measure the allocated heap size by jemalloc.

Be aware that you can get different results with different allocators.


Regarding Github: "This crate is not maintained and is no longer used by Servo. At the time of writing, Servo uses internal malloc_size_of instead."

You can either use heapsize crate or you can check the implementation details of malloc_size_of as well

Upvotes: 5

Related Questions