This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

STL maps never deallocate


I'm not sure by the dearth of comments on memory leaks in STL whether
my question was inappropriate, uninteresting, or been repeatedly
discussed.  I wonder whether anyone is currently working on STL.

Anyway, I found at SGI's site 

  In the default STL allocator, memory allocated for blocks of small
  objects is not returned to malloc. It can only be reused by subsequent
  allocate requests of (approximately) the same size. Thus programs that
  use the default may appear to leak memory when monitored by certain
  kinds of simple leak detectors.  This is intentional. ...  Like the HP
  allocators, it does not maintain the necessary data structures to free
  entire chunks of small objects when none of the contained small
  objects are in use. This is an intentional choice of execution time
  over space use. It may not be appropriate for all programs. On many
  systems malloc_alloc may be more space efficient, and can be used when
  that is crucial.

Thus, if you have a program that allocates large numbers of small
things, then using malloc_alloc solves this problem.

David Kulp writes:
 > It appears the STL maps (and probably other similar templates) never
 > free allocated memory.  Is there some secret "purge" routine to tell
 > STL to free its free store?  Perhaps more to the point, isn't this a
 > an STL bug?
 > 
 > For example, in the following code I create a map of 500K elements.
 > At the stub statement at (1), a simple examination of allocated memory
 > using top shows that about 12MB are allocated.  However, stepping
 > through the remaining lines as the map object is supposedly
 > deallocated as it's popped off the stack shows that memory is never
 > freed, but remains on the STL's own free store.  (This is not the
 > case, e.g., for vectors.)
 > 
 > #include <map>
 > struct intcmp
 > {
 >   bool operator()(const int i1, const int i2) const
 >   {
 >     return i1 < i2;
 >   }
 > };
 > 
 > int
 > foo() {
 >   int i;
 >   map<int,int,intcmp> m;
 >   for (i=0; i<500000; i++)
 >     m[i] = i;
 >   i = 0;                            <--1
 > }                                   <--2
 > 
 > int
 > main() {
 >   foo();
 > }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]