简体   繁体   中英

How to measure total STL container memory consumption?

As in the title: given an object of type of some STL container (eg std::vector<int> or std::set<MyClass> ) I would like to know their memory consumption --- that is --- how much memory is consumed to store the elements, the auxiliary data for each element and the container size. I assume that the objects stored do not allocate any additional memory.

For a std::vector<int> v I can add:

sizeof(std::vector<int>) + v.capacity()*sizeof(int)

because vectors do not store any auxiliary data per element. But how can I do it for other containers?

I can live with non-constant time complexity.

Create your own STL allocator and track the size of the memory requests placed to it, then jsut add the size of the container itself. This article gives a good overview of creating one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM