STL容器的内存消耗 [英] Memory consumption by STL containers

查看:202
本文介绍了STL容器的内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中我计划使用几个STL容器。如果内存消耗达到阈值,应用程序将采取某些步骤。为此,我需要对STL容器使用多少内存执行接近准确的计算。

  vector< string> StringList 
map< string,int> mapstring

这是我估计记忆的方式:



对于 StringList 的大小,循环遍历向量的所有元素,并继续添加字符串大小。

  string size = sizeof(string)+ string.capacity()* sizeof(char)

然后最后添加到 sizeof(StringList);



循环容器的所有键,并继续添加字符串大小,然后添加int的大小 mapstring.size()* sizeof(int)。然后最后添加到这个 sizeof(mapstring);



我想一个更好的方法将指定自己的分配器类和跟踪内存中的内存使用,但编写一个可能是不平凡的。这个估计看起来不错吗?

解决方案

对于 std :: vector std :: string ,容量,而不是大小,将
是一个更好的近似。对于基于节点的容器( std :: set
等),你需要乘以节点数(大约
个元素)乘以每个节点的大小。然而,这只是准确的,如果
分配器不使用优化的池分配器为节点。



但是,如果你真的想知道使用多少内存,一个
更好的策略是替换全局运算符new
操作符delete ,并跟踪实际分配。更多的
可以替换 malloc free 。正式地,这不是
允许的,但在实践中,我从来没有遇到一个实现,其中
它不工作。另一方面,如果你替换 malloc free
,你必须实现实际的内存管理自己。如果
替换 operator new operator delete ,可以使用 malloc
免费,这使得它相当微不足道。



每个分配有一些固定的开销。 A 100000
每个10字节的分配将比
消耗显着更多的内存,每个分配100000字节。


I am working on an application in which I am planning to use couple of STL containers. The application will take certain steps if memory consumption reaches a threshold. For that purpose I need to perform a close to accurate calculation on how much memory is used by STL containers.

vector<string> StringList
map<string, int> mapstring

This is how I am estimating memory:

For size of StringList, loop over all the elements of the vector and keep adding the string sizes.

string size = sizeof(string) + string.capacity()*sizeof(char)

Then finally add to this sizeof(StringList);

For size of mapstring, loop over all the keys of the container and keep adding the string sizes and then add the sizes of int which is mapstring.size()*sizeof(int). Then finally add to this sizeof(mapstring);

I guess a better approach would be specifying own allocator class and keeping track of memory usage inside it but writing one could be non-trivial. Does this estimation look good ?

解决方案

For std::vector and std::string, capacity, rather than size, would be a better approximation. For node based containers (std::set, etc.), you'd want multiply the number of nodes (roughly the number of elements) times the size of each node. This only accurate, however, if the allocator doesn't use an optimized pool allocator for the nodes.

If you really want to know how much memory is being used, however, a better strategy would be to replace the global operator new and operator delete, and keep track of the actual allocations. Even more accurate would be to replace malloc and free. Formally, this is not allowed, but in practice, I've never encountered an implementation where it doesn't work. On the other hand, if you replace malloc and free, you have to implement the actual memory management yourself. If you replace operator new and operator delete, you can use malloc and free, which makes it fairly trivial.

Note too that each allocation has some fixed overhead. A 100000 allocations of 10 bytes each will consume significantly more memory than 10 allocations of 100000 bytes each.

这篇关于STL容器的内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆