How can i estimate memory usage of std::map?(我如何估计 std::map 的内存使用量?)
问题描述
例如,我有一个已知 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 的内存使用量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我如何估计 std::map 的内存使用量?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
