使用std::map时的内存增长
Memory growth when using std::map
我正在观察这段代码的内存增长。但是它应该不会显示内存增长,因为所有创建的对象都被删除了。
我已经为MyObj实现了析构函数,并且通过gdb观察到,当映射迭代时,实际上调用了析构函数。谁能解释一下我哪里做错了?
map<int,MyObj*> myMap;
for(int i = 1; i<= 500000; i ++)
{
MyObj* pMyObj = new MyObj;
myMap.insert(pair<int,MyObj*>(i,pMyObj));
}
map<int,MyObj*>::iterator ite = myMap.begin();
while (ite != myMap.end())
{
delete ite->second;
ite++;
}
myMap.clear();
您可能想要阅读http://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html#Freeing-after-Malloc。尤其是最后几段:
有时候,free可以将内存返回给操作系统让这个过程更小。通常,它所能做的就是允许以后调用malloc来重用空间。与此同时,空间依然存在在你的程序中,作为malloc内部使用的自由列表的一部分。
如果您希望将大块内存返回给操作系统,请考虑使用mmap
。
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 使 std::vector 分配对齐内存的现代方法
- std::unordered_map析构函数不释放内存?
- std::p romise::set_value() 和 std::future::wait() 是否提供内存围栏?
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 在共享缓冲区内存中创建 ::std::string 对象
- 内存未释放 std::list<std::shared_ptr<std::string>> C++
- <char> 使用 Vulkan 映射内存时如何使用 std::vector 而不是 void**?
- 我可以将新的 std::tuple 放入内存映射区域,并在以后读回吗?
- 如何防止使用 std::shared_ptr 的代码中的内存泄漏
- C++ 中 std::vector 的内存问题
- std::set 是否将对象连续存储在内存中?
- 如何在窗口之间移动 std::unique_ptr 而不会冒内存泄漏的风险?
- 为什么"std::uninitialized_copy"通常取消对未初始化内存的迭代器的引用不是未定
- vector是否为std::移动的对象连续分配内存
- Valgrind 在 std::make_unique 中显示内存泄漏
- std::initializer_list 堆是否分配内存?
- windows中的IPC共享内存std::vector