This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
multimap.clear() does not free its memory
- From: Toon Knapen <toon dot knapen at fft dot be>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 07 Apr 2005 11:23:37 +0200
- Subject: multimap.clear() does not free its memory
After filling up an std::multimap I print the memory consumed by my
process (currently using mallinfo() which is probably not the best way
to do it but anyway I verified in /proc and there the same numbers appear).
Next I clear() the multimap and I see that the consumed memory is still
the same. Afterwards I create a new map of the same size but the memory
consumption does not go up (as expected) but after the clear again the
memory consumption does not go down either.
So I guess the memory handler is being smart and does lazy-delete the
memory allocated when filling up the multimap. This is why the second
multimap does not take 'extra' space.
However after deleting the second map and allocating 1M chars, the
memory consumption goes up with 1M bytes and thus the memory that
'should be' freed when deleting the multimap is not re-used by the
character-array.
So finally I want to 'force' the memory used by the multimap to become
_really_ free but how can I do that.
I'm using gcc 3.2.2 on linux 2.4.18 on an old pentium 3 but observe the
same with gcc-3.4 and with gcc on an altix-sp3 machine.
Below is my test-program and the output generated on my machine
<begin program>
#include <map>
#include <malloc.h>
#include <iostream>
void report_mem()
{
static struct mallinfo l_mallinfo ;
l_mallinfo = mallinfo();
std::cout << "mem: " << l_mallinfo.uordblks + l_mallinfo.hblkhd <<
std::endl ;
}
int main()
{
report_mem() ;
{
std::multimap< int, int > map ;
for(int i = 0 ; i < 100000 ; ++i ) {
map.insert( std::pair< int, int >( 0, 0 ) ) ;
}
std::cout << "\nfirst map is filled" << std::endl ;
report_mem() ;
map.clear() ;
std::cout << "\nfirst map is cleared" << std::endl ;
report_mem() ;
}
{
std::multimap< int, int > map ;
for(int i = 0 ; i < 100000 ; ++i ) {
map.insert( std::pair< int, int >( 0, 0 ) ) ;
}
std::cout << "\nsecond map is filled" << std::endl ;
report_mem() ;
map.clear() ;
std::cout << "\nsecond map is cleared" << std::endl ;
report_mem() ;
}
std::cout << "\nAll maps should be destroyed" << std::endl ;
report_mem() ;
new char[1000000]
report_mem() ;
return 0 ;
}
<end program>
<begin output>
mem: 0
first map is filled
mem: 2497184
first map is cleared
mem: 2497184
second map is filled
mem: 2497184
second map is cleared
mem: 2497184
All maps should be destroyed
mem: 2497184
mem: 3500704
<end output>
Thanks for any help,
Toon