| View previous topic :: View next topic |
| Author |
Message |
Peter Klotz Guest
|
Posted: Thu Jul 03, 2003 8:42 pm Post subject: Borland C++ 5.5.1 STL memory leak |
|
|
The program below shows a massive memory leak when combining std::map and std::list.
First the program needs 85MB for storage where MSVC only needs 7MB.
Second the clear() method does not free any memory. The program still needs 85MB of RAM.
Just compile the program and observe it with the Task Manager.
Best regards, Peter.
#include <map>
#include <list>
#include <string>
#include <iostream>
#include <windows.h>
int main(void)
{
std::map<int,std::list data;
for (int i=0;i<50000;++i)
data[i].push_back("test");
std::cout << "before clear" << std::endl; // 85MB needed
::Sleep(5000);
data.clear();
std::cout << "after clear" << std::endl; // still 85MB needed
::Sleep(5000);
return 0;
}
|
|
| Back to top |
|
 |
Alex Bakaev [TeamB] Guest
|
Posted: Thu Jul 03, 2003 9:38 pm Post subject: Re: Borland C++ 5.5.1 STL memory leak |
|
|
Peter Klotz wrote:
| Quote: | The program below shows a massive memory leak when combining std::map and std::list.
First the program needs 85MB for storage where MSVC only needs 7MB.
Second the clear() method does not free any memory. The program still needs 85MB of RAM.
Just compile the program and observe it with the Task Manager.
|
The task manager doesn't reflect actual memory usage/leaks properly. At
least not in the kind of code that you present. All compilers have
sub-allocators and they may not return the previously allocated memory
to the OS.
..a
|
|
| Back to top |
|
 |
Peter Klotz Guest
|
Posted: Fri Jul 04, 2003 7:11 am Post subject: Re: Borland C++ 5.5.1 STL memory leak |
|
|
"Alex Bakaev [TeamB]" <zxtt (AT) att (DOT) net> wrote:
| Quote: | Peter Klotz wrote:
First the program needs 85MB for storage where MSVC only needs 7MB.
All compilers have sub-allocators and they may not return
the previously allocated memory to the OS.
|
I see your point. The memory is kept in a pool and reused.
Just one comment about the memory usage. 85MB means that a map entry containing a single-element string list needs almost 1800 Bytes of RAM. Isn't that quite a lot?
Best regards, Peter.
|
|
| Back to top |
|
 |
Peter Klotz Guest
|
Posted: Fri Jul 04, 2003 5:55 pm Post subject: Re: Borland C++ 5.5.1 STL memory leak |
|
|
[email]maeder (AT) glue (DOT) ch[/email] (Thomas Maeder [TeamB]) wrote:
| Quote: | "Peter Klotz" <peter.klotz (AT) aon (DOT) at> writes:
Second the clear() method does not free any memory.
It's not supposed to. You have the best chances of disposing of the memory
used by a map without destructing it by doing:
std::map<int,std::list().swap(data);
This will change data's guts with those of a default constructed temporary
map; the temporary will dispose of data's original guts in its destructor.
|
I tried this method but the memory remains allocated. The program still uses 85MB after the above line.
|
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Fri Jul 04, 2003 7:31 pm Post subject: Re: Borland C++ 5.5.1 STL memory leak |
|
|
"Peter Klotz" <peter.klotz (AT) aon (DOT) at> writes:
| Quote: | I tried this method but the memory remains allocated. The program still
uses 85MB after the above line.
|
I may have been unclear. The memory may still be held by the program; it's
just no longer held by the map.
|
|
| Back to top |
|
 |
|