我如何估计 std::map 的内存使用量?

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

问题描述

限时送ChatGPT账号..

例如,我有一个已知 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 的内存使用量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
STL BigInt class implementation(STL BigInt 类实现)
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)