BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Borland C++ 5.5.1 STL memory leak

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Command Line Tools)
View previous topic :: View next topic  
Author Message
Peter Klotz
Guest





PostPosted: Thu Jul 03, 2003 8:42 pm    Post subject: Borland C++ 5.5.1 STL memory leak Reply with 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.

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





PostPosted: Thu Jul 03, 2003 9:38 pm    Post subject: Re: Borland C++ 5.5.1 STL memory leak Reply with quote



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





PostPosted: Fri Jul 04, 2003 7:11 am    Post subject: Re: Borland C++ 5.5.1 STL memory leak Reply with quote




"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





PostPosted: Fri Jul 04, 2003 5:55 pm    Post subject: Re: Borland C++ 5.5.1 STL memory leak Reply with quote


[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





PostPosted: Fri Jul 04, 2003 7:31 pm    Post subject: Re: Borland C++ 5.5.1 STL memory leak Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Command Line Tools) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.