How can I get the full memory footprint behind a struct?

I have a Vec that contains an arbitrary number of structs, each of which has two strings. I would like to know the total memory consumed. Not just size_of::<Vec<T>>() or size_of::<T>() * n, but including both the stack and heap size of Strings and other relevant structs.

How can I do this? Or is it not worth my effort?

Add up the capacities of the strings and add it to size_of::<T>() * vec.capacity() + size_of::<Vec<T>>().

1 Like

Thanks! I thought that would work, but for some reason it just didn't seem right.

There is a post of @matklad about the problem in general: Measuring Memory Usage in Rust (rust-analyzer.github.io).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.