如何强制我的std :: map释放内存使用? [英] How do I force my std::map to deallocate memory used?

查看:1528
本文介绍了如何强制我的std :: map释放内存使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是std :: map,我似乎不能释放内存回到操作系统。它看起来像,

I'm using a std::map, and I can't seem to free the memory back to the OS. It looks like,

int main(){
  aMap m;

  while(keepGoing){
    while(fillUpMap){
       //populate m
    }
    doWhatIwantWithMap(m);
    m.clear();//doesnt free memory back to OS

    //flush some buffered values into map for next iteration
    flushIntoMap(m);
  }
}



每个(fillUpmap)分配大约1gig, m非常感兴趣的是让它回到我的系统之前,它吃了我所有的记忆。

Each (fillUpmap) allocates around 1gig, so I'm very much interested in getting this back to my system before it eats up all my memory.

Ive经历了同样的std :: vector,但我可以强制通过使用空的std :: vector进行交换来释放。这不适用于地图。

Ive experienced the same with std::vector, but there I could force it to free by doing a swap with an empty std::vector. This doesn't work with map.

当我使用valgrind它说,所有内存释放,所以它不是一个泄漏的问题,因为一切都很好地清除

When I use valgrind it says that all memory is freed, so its not a problem with a leak, since everything is cleared up nicely after a run.

编辑:

清除后必须出现冲洗。

推荐答案

m.clear()将内存释放回堆,实现不释放回到操作系统(即使他们可以,诸如碎片的问题使它很难)。

m.clear() releases memory back to the heap, but it's common for heap implementations to not release that back to the OS (even when they can, issues such as fragmentation make it hard).

这是默认分配器如何工作,如果你' ve为地图指定了自己的分配器,它可能有自己的缓存。但是,即使在这种情况下,它也应该被缓存,以便立即重用。

This is how the default allocator works, if you've specified your own allocator for the map, it might have its own cache. However, even in that case, it should be cached in order to be reused immediately anyway.

地图没有容量vs大小矢量的概念。

Maps don't have a concept of capacity vs size the way vectors do.

这篇关于如何强制我的std :: map释放内存使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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