std::deque 在程序退出之前不会释放内存

发布于 2024-11-04 19:24:13 字数 1093 浏览 3 评论 0原文

在 Linux 上,std::deque 在程序退出之前不会释放内存。完整的代码如下。任何帮助将不胜感激!

#include <deque>
#include <vector>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <queue>
#include <list>
#include <cstdio>
#include <cstdlib>

typedef  boost::shared_ptr<std::vector<int> > VecPtr;
typedef  std::deque< VecPtr  > QueueType;

 char buf[1024];
 char line[1024];

 int main()
 {

  {

    int v=0;
    QueueType  deq;
    for(int i=0; i<30;++i)
    for(int j=0; j<1000;++j)
    for(int k=0;k<1000;++k)
    {
       VecPtr p( new std::vector<int>);
       deq.push_back(p);
    }

    std::cout<<"Done with increasing deq:deq size="<<deq.size()<<std::endl;
    sleep(20);

    std::cout<<"start decreasing deq size"<<std::endl;
    while(deq.size()>0)
    {
      deq.pop_front();
    }
    std::cout<<"done with decreasing deq size,deq size="<<deq.size()<<std::endl;
  }
  std::cin.getline(line,sizeof(line));
  return 0;
}

On linux, std::deque does not release memory until program exits. The complete code is below. Any help will be greatly appreciated!

#include <deque>
#include <vector>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <queue>
#include <list>
#include <cstdio>
#include <cstdlib>

typedef  boost::shared_ptr<std::vector<int> > VecPtr;
typedef  std::deque< VecPtr  > QueueType;

 char buf[1024];
 char line[1024];

 int main()
 {

  {

    int v=0;
    QueueType  deq;
    for(int i=0; i<30;++i)
    for(int j=0; j<1000;++j)
    for(int k=0;k<1000;++k)
    {
       VecPtr p( new std::vector<int>);
       deq.push_back(p);
    }

    std::cout<<"Done with increasing deq:deq size="<<deq.size()<<std::endl;
    sleep(20);

    std::cout<<"start decreasing deq size"<<std::endl;
    while(deq.size()>0)
    {
      deq.pop_front();
    }
    std::cout<<"done with decreasing deq size,deq size="<<deq.size()<<std::endl;
  }
  std::cin.getline(line,sizeof(line));
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-11-11 19:24:13

是的,pop_front() 不会释放由 push_back() 分配的存储空间
如果你想在程序结束之前释放它,你可以结束该对象的生命周期。如果您想在对象的生命周期结束之前取消分配它,请考虑使用“

QueueType().swap (deq); // C++98
deq.shrink_to_fit(); // C++11

That is correct, pop_front() does not deallocate storage that was allocated by push_back()
If you want to deallocate it before the program ends, you can end the lifetime of the object. If you want to deallocate it before the lifetime of the object ends, consider using a "shrink-to-fit" idiom for C++ container classes.

QueueType().swap (deq); // C++98
deq.shrink_to_fit(); // C++11
动次打次papapa 2024-11-11 19:24:13

复制 MSalters 关于 如何从 std::deque 释放内存? 的响应(感谢埃米尔·科米尔提供的链接)。

“std::deque 会将内存返回给它的分配器。通常这个分配器不会将内存返回给操作系统。在这种情况下,看起来好像内存没有“释放”。只要满足以下条件,好的内存泄漏检测器就会得到满足:内存被返回给分配器,并了解并非所有内存都被 free() 释放。”

因此,即使它释放了内存,它也没有真正释放内存。这很容易被认为是不合理的行为,除非程序中的所有分配都是由STL执行的;对图书馆相当自恋。因此,请考虑覆盖任何内存密集型数据结构的分配器,以改进控制。其他人也发现 STL 分配器系统也缺乏 - 请参阅 Electronic Arts 的 EASTL 项目。

Copying a response from MSalters on How to release memory from std::deque? (thanks to Emile Cormier for the link).

"std::deque will return memory to its allocator. Often this allocator won't return the memory to the OS. In such cases, it appears as if memory is not "released". Good memory leak detectors will be satisfied as soon as memory is returned to the allocator, and understand that not all memory is released by free()."

So even when it frees memory, it doesn't really free memory. This is easily regarded as unreasonable behavior, unless all allocation in the program is performed by STL; rather narcissistic of the library. Therefore consider overriding the allocators for any memory-intensive data structures, to improve control. Others have found the STL allocator system lacking as well - see the EASTL project from Electronic Arts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文