关于boost::unorder_map析构后内存不释放的问题

Kaile 2011-11-01 08:08:29
现在遇到一个比较头疼的问题:

类似于这样的一个hash_map:
typedef boost::unordered_map<string, int> hashmap_str;

hashmap_str* pMap = new hashmap_str();


不停地插入数据一万次以后,程序消耗了几百M内存,现在想把这些数据写入到数据库中, 然后
delete pMap;

结果发现程序占用内存并没有下降。

因为插入数据可能会一直持续不断,等到程序消耗了1.3G内存左右时就会崩溃。

所以请请教一下高手,这种问题如何解决?

网上查了一下,有人说是默认allocator不释放内存, 那么用哪种allocator才能把内存释放给操作系统?

先谢了。


...全文
605 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kaile 2011-11-03
  • 打赏
  • 举报
回复
问题已解决,原来是一个第三方库在退出时没有释放资源,可悲的是我一直注意力放在unorder_map上,白白浪费了3天时间。

结了。
cgl_lgs 2011-11-01
  • 打赏
  • 举报
回复
智能指针可以调用release来提前释放(好像叫这个名字吧,忘了)
柯本 2011-11-01
  • 打赏
  • 举报
回复
如果不能找到合适的解决方案(boost有时太复杂了),试试用以下SharedMemory方法,看看能不能解决你的问题:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#include <boost/unordered_map.hpp> //boost::unordered_map
#include <functional> //std::equal_to
#include <boost/functional/hash.hpp> //boost::hash

int main ()
{
using namespace boost::interprocess;
//Erase previous shared memory with the name
shared_memory_object::remove("MySharedMemory");

try{
//Create shared memory
managed_shared_memory segment(create_only ,"MySharedMemory" ,65536);

//Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
//so the allocator must allocate that pair.
typedef int KeyType;
typedef float MappedType;
typedef std::pair<const int, float> ValueType;

//Typedef the allocator
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

//Alias an unordered_map of ints that uses the previous STL-like allocator.
typedef boost::unordered_map
< KeyType , MappedType
, boost::hash<KeyType> ,std::equal_to<KeyType>
, ShmemAllocator>
MyHashMap;

//Construct a shared memory hash map.
//Note that the first parameter is the initial bucket count and
//after that, the hash function, the equality function and the allocator
MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap") //object name
( 3, boost::hash<int>(), std::equal_to<int>() //
, segment.get_allocator<ValueType>()); //allocator instance

//Insert data in the hash map
for(int i = 0; i < 100; ++i){
myhashmap->insert(ValueType(i, (float)i));
}
}
catch(...){
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}


Kaile 2011-11-01
  • 打赏
  • 举报
回复
先用
pMap->clear();

已经试过了,没有效果。

用智能指针,将new的对象放到智能指针shared_ptr中,std::trl::shared_ptr<Widget>pMap

智能指针是不需要的时候才析构,我现在想通过手工析构来释放内存,智能指针好象与我的目的不相符吧
cocoabird 2011-11-01
  • 打赏
  • 举报
回复
用智能指针,将new的对象放到智能指针shared_ptr中,std::trl::shared_ptr<Widget>pMap
柯本 2011-11-01
  • 打赏
  • 举报
回复
先用
pMap->clear();

delete pMap;
试试?

dengqibin 2011-11-01
  • 打赏
  • 举报
回复
那你插完有erase了吗
Kaile 2011-11-01
  • 打赏
  • 举报
回复
不停地插入数据一万次

new 只是一次
dengqibin 2011-11-01
  • 打赏
  • 举报
回复
不停的hashmap_str* pMap = new hashmap_str();new这么多做什么啊?

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧