简体   繁体   English

如何衡量总STL容器内存消耗?

[英]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. 如标题所示:给定一个STL容器类型的对象(例如std::vector<int>std::set<MyClass> )我想知道它们的内存消耗---即---多少使用内存来存储元素,每个元素的辅助数据和容器大小。 I assume that the objects stored do not allocate any additional memory. 我假设存储的对象不分配任何额外的内存。

For a std::vector<int> v I can add: 对于std::vector<int> v我可以添加:

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. 创建自己的STL分配器并跟踪放置到它的内存请求的大小,然后jsut添加容器本身的大小。 This article gives a good overview of creating one. 本文概述了创建一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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