昨天我尝试使用 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 非常高的内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
读取输入文件,最快的方法?read input files, fastest way possible?(读取输入文件,最快的方法?)
在 C++ 中读取格式化输入的最简单方法?The easiest way to read formatted input in C++?(在 C++ 中读取格式化输入的最简单方法?)
从 .txt 文件读取到 C++ 中的二维数组Reading from .txt file into two dimensional array in c++(从 .txt 文件读取到 C++ 中的二维数组)
如何在 C++ 中模拟按键按下How to simulate a key press in C++(如何在 C++ 中模拟按键按下)
为什么在 cin.ignore() 之后没有 getline(cin, var) 读取Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(为什么在 cin.ignore() 之后没有 getline(cin, var) 读取
scanf 格式输入的 cin 类比是什么?What is the cin analougus of scanf formatted input?(scanf 格式输入的 cin 类比是什么?)