本文介绍了我如何估计std :: map的内存使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个std :: map已知的sizeof(A)和sizeof(B),而map有N个条目。你如何估计它的内存使用?
我会说这是像

For example, I have a std::map with known sizeof(A) and sizeof(B), while map has N entries inside. How would you estimate its memory usage?I'd say it's something like

(sizeof(A) + sizeof(B)) * N * factor

但是什么是因素?不同的公式可能?

But what is the factor? Different formula maybe?

也许更容易要求上限?

推荐答案

估算值会更接近

(sizeof(A) + sizeof(B) + ELEMENT_OVERHEAD) * N + CONTAINER_OVERHEAD

您添加的每个元素都有开销,并且还有一个固定的开销用于维护数据结构用于存储地图的数据结构。这通常是二叉树,例如。例如,在GCC C ++ STL实现 ELEMENT_OVERHEAD 将是 sizeof(_Rb_tree_node_base) CONTAINER_OVERHEAD 会是 sizeof(_Rb_tree)。对于上图,您还应该添加用于存储地图元素的内存管理结构的开销。

There is an overhead for each element you add, and there is also a fixed overhead for maintaining the data structure used for the data structure storing the map. This is typically a binary tree, such as a Red-Black Tree. For instance, in the GCC C++ STL implementation ELEMENT_OVERHEAD would be sizeof(_Rb_tree_node_base) and CONTAINER_OVERHEAD would be sizeof(_Rb_tree). To the above figure you should also add the overhead of memory management structures used for storing the map's elements.

通过测量各种大型集合的代码的内存消耗,可能更容易得出估计值。

It's probably easier to arrive at an estimate by measuring your code's memory consumption for various large collections.

这篇关于我如何估计std :: map的内存使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:37