std::unordered_map very high memory usage(std::unordered_map 非常高的内存使用率)
问题描述
昨天我尝试使用 std::unordered_map 这段代码让我迷惑了它使用了多少内存.
Yesterday i tried to use std::unordered_map and this code confused me how much memory it used.
typedef list<string> entityId_list;
struct tile_content {
char cost;
entityId_list entities;
};
unordered_map<int, tile_content> hash_map;
for (size_t i = 0; i < 19200; i++) {
tile_content t;
t.cost = 1;
map[i] = t;
}
所有这部分代码都是在 MS VS2010 中以调试模式编译的.我在我的任务管理器中看到的是大约 1200 kb 的干净"进程,但是在填充 hash_map 后它使用了 8124 kb 的内存.这是 unordered_map 的正常行为吗?为什么要使用这么多内存?
All this parts of code was compiled in MS VS2010 in debug mode.
What I've been seen in my task manager was about 1200 kb of "clean" process, but after filling hash_map it uses 8124 kb of memory. Is it normal behavior of unordered_map? Why so much memory used?
推荐答案
unordered_map 结构旨在以一种使添加、删除、查找和无序遍历高效的方式保存大量对象.对于小型数据结构,它并不意味着内存高效.为了避免与调整大小相关的惩罚,它在第一次创建时分配了许多哈希链头.
The unordered_map structure is designed to hold large numbers of objects in a way that makes adds, deletes, lookups, and orderless traverses efficient. It's not meant to be memory-efficient for small data structures. To avoid the penalties associated with resizing, it allocates many hash chain heads when it's first created.
这篇关于std::unordered_map 非常高的内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::unordered_map 非常高的内存使用率
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
