简体   繁体   English

std :: unordered_map的内存使用率很高

[英]std::unordered_map very high memory usage

Yesterday i tried to use std::unordered_map and this code confused me how much memory it used. 昨天我尝试使用std::unordered_map ,此代码使我感到困惑,它使用了多少内存。

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;
}

All this parts of code was compiled in MS VS2010 in debug mode. 所有这些代码部分都是在MS VS2010中以调试模式编译的。 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. 我在任务管理器中看到的是大约1200 kb的“干净”进程,但是在填充hash_map它将使用8124 kb的内存。 Is it normal behavior of unordered_map ? 这是unordered_map正常行为吗? Why so much memory used? 为什么要使用那么多的内存?

The unordered_map structure is designed to hold large numbers of objects in a way that makes adds, deletes, lookups, and orderless traverses efficient. unordered_map结构旨在以使添加,删除,查找和无序遍历有效的方式来容纳大量对象。 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. 为了避免与调整大小相关的惩罚,它在首次创建时会分配许多哈希链头。

That's roughly 6MB for ~20k objects, so 300 bytes per object. 大约2万个对象大约需要6MB,因此每个对象300个字节。 Given the hash table may well be sized to have several times more buckets than current entries, each bucket may itself be a pointer to a list or vector of colliding objects, each heap allocation involved in all of that has probably been rounded up to the nearest power of two, and you've got debug on which may generate some extra bloat, it all sounds about right to me. 假设哈希表的大小可能比当前条目具有更多的存储桶,则每个存储桶本身可能是指向碰撞对象列表或向量的指针,所有涉及的每个堆分配可能已四舍五入到最接近的值。 2的幂,并且已经调试过,可能会产生一些额外的膨胀,这一切对我来说都是正确的。

Anyway, you're not going to get sympathy for the memory or CPU efficiency of anything in debug build ;-P. 无论如何,您不会对调试版本;-P中的任何内容的内存或CPU效率感到同情。 Microsoft can inject any slop they like in there, and the user has no right of expectations around performance. Microsoft可以在其​​中注入他们喜欢的任何倾斜,并且用户无权获得对性能的期望。 If you find it's bad in an optimised build, then you've got something to talk about. 如果您发现在优化的版本中不好,那么您有什么要谈的。

More generally, how it scales with size() is very important, but it's entirely legitimate to wonder how a program would go with a huge number of relatively small unordered maps. 更一般而言,它如何与size()缩放非常重要,但是完全可以怀疑一个程序如何与大量相对较小的无序映射一起使用。 Worth noting that below a certain size() even brute force searches in a vector, binary searches in a sorted vector, or a binary tree may out-perform an unordered map, as well as being more memory efficient. 值得一提的是,在某个size()甚至矢量中的蛮力搜索,排序后的矢量中的二进制搜索或二叉树都可能胜过无序映射,并且存储效率更高。

This doesn't necessarily mean that the hash map uses so much memory, but that the process has requested that much memory from the OS. 这并不一定意味着哈希映射使用了如此多的内存,而是该进程已从操作系统请求了那么多的内存。

This memory is then used to satisfy malloc/new requests by the program. 然后,该内存将用于满足程序的malloc / new请求。 Some (or most, I am not sure about this) memory allocators require more memory from the OS than needed at that point in time for efficiency. 为了提高效率,某些(或大多数,我对此不太确定)内存分配器需要的操作系统内存比当时需要的更多。

To know how much memory is used by the unordered_map I would use a memory profiler like perftools . 要知道unordered_map使用了多少内存,我将使用perftools之类的内存分析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM