std :: unordered_map非常高的内存使用 [英] std::unordered_map very high memory usage

查看:2091
本文介绍了std :: unordered_map非常高的内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我试图使用unordered_map,这个代码混淆了我使用多少内存。

Yesterday i tried to use 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非常高的内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆