ggsddu 19 Newbie Poster

hey tux4life,

i recently meet the same problem, so i go to google, with the query "stl map memory deallocate", and this page is the 1st result given by google. But i found the answers in the thread not solve the problem. so i try to solve it myself.

i think i should share my finding to everyone else, no matter how old this thread is.

tux4life commented: I like your way of thinking (if 'your finding' solves the problem :P) +19
ggsddu 19 Newbie Poster

hi DarkNova,
i test your example code in my redhat server, also with g++ 3.4.6.
in fact, i found below things, which may be useful to you:
1) it's not the STL cache the memory, but is the 'malloc' in glibc.
2) you can see the malloc status with the function 'malloc_stats()' or 'mallinfo()', which are defined in <malloc.h>
3) i run your example, and my result is as below:

void testmap()
{
/*
*  <====   at this point, malloc_stats() tell me that: 
*       system bytes     =          0
*       in use bytes     =          0
*/
  map<int, float> testmap;
  for (int i = 0; i < 1000000; i++) {
    testmap[i] = (float)i;
  }
/*
*  <====   at this point, malloc_stats() tell me that: 
*       system bytes     =          48144384
*       in use bytes     =          48005120
*/
  testmap.clear();
/*
*  <====   at this point, malloc_stats() tell me that: 
*       system bytes     =          48140288    <==== malloc cache the memory here
*       in use bytes     =          5120
*/
}

4) in my test, testmap() and testvect() perform the same, i don't know why your testmap() and testvect() perform differently. If you want to get more detail infomation about how malloc work, please see the chepter 3 in glibc manual. (http://www.gnu.org/software/libc/manual/)