问题描述
例如,我有一个已知 sizeof(A) 和 sizeof(B) 的 std::map,而 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 的内存使用量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)