6

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;
}
wu3mei
  • 61
  • 1
  • 2
  • @Jon There are no question marks in the title or the body! Amazing :) – Marlon Apr 29 '11 at 16:39
  • possible duplicate of [How to release memory from std::deque?](http://stackoverflow.com/questions/1242357/how-to-release-memory-from-stddeque) – Emile Cormier Apr 29 '11 at 16:45

2 Answers2

19

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
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Maybe I'm missing something, but shouldn't `deq` go out of scope, be popped off the stack and destructed before the `cin`? Since pop_front will destruct the shared_ptr, shouldn't the memory allocated with new be freed? The question is not clear so I'm not sure how @wu3mei is confirming that the memory is not released (is he/she implying a leak?, implying the memory is not released back to the OS?) – hifier Apr 30 '11 at 06:04
  • @objectiveGeek Yes, once `deq` goes out of scope, all memory is released correctly. The OP's concern was that is was not released "until program exits", likely implying the intent to undo the allocations made by `push_back()` some time before the end of the program is reached. – Cubbi Apr 30 '11 at 13:32
0

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.

Community
  • 1
  • 1
MichaelsonBritt
  • 976
  • 6
  • 9