C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Free memory problem in STL List

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Kathy
Guest





PostPosted: Sat Sep 27, 2003 2:05 pm    Post subject: Free memory problem in STL List Reply with quote



Hi:

I write a simple C++ program using the following platform

OS: Linux Redhat 7.3
GCC: gcc-3.2.1
C++: libstdc++-v3

The part of the source code of my program is:

list<double> clList1;
double d=1.0;
for (long i=0; i<100000; i++)
{
clList1.push_back(d);
d++;
}
clList1.clear();

list float f=1.0;
for (long j=0; j<1000000; j++)
{
clList2.push_back(f);
f++;
}
clList2.clear();

list d=1.0
for (long k=0; k<100000; k++)
{
clList3.push_back(d);
d++;
}
clList3.clear();

After I compiled the program and run the EXE file, I found that the program
cannot free the used memory after calling list::clear() function. Also, the
used memory kept alive until the program quit.

Actually, I need to use many lists to temporarily store different data when
running the program. And, my program runs as a server. Therefore, my program
does not exit after finishing its job.

As a result, the memory used by my program increases sharply after finishing
several job requests from clients.

Do anyone get any idea to solve this memory usage problem when using this
STL Container in my program?

Note: When I run the same program in Microsoft Window (using Visual Studio
6.0 for development), I did not encounter this problem. Is it a problem of
the implementation of C++ STL Container in Linux?

Kathy



Back to top
Gianni Mariani
Guest





PostPosted: Sat Sep 27, 2003 3:30 pm    Post subject: Re: Free memory problem in STL List Reply with quote



Kathy wrote:
....
Quote:

After I compiled the program and run the EXE file, I found that the program
cannot free the used memory after calling list::clear() function. Also, the
used memory kept alive until the program quit.

That is the expected behaviour.

Quote:

Actually, I need to use many lists to temporarily store different data when
running the program. And, my program runs as a server. Therefore, my program
does not exit after finishing its job.

As a result, the memory used by my program increases sharply after finishing
several job requests from clients.

Do anyone get any idea to solve this memory usage problem when using this
STL Container in my program?

2 options.

1. Perform computations in a different process.
2. Use a different allocator (STL allows this), that returns resources
to the OS.

Quote:

Note: When I run the same program in Microsoft Window (using Visual Studio
6.0 for development), I did not encounter this problem. Is it a problem of
the implementation of C++ STL Container in Linux?

Neither - this is expected behaviour.


Back to top
David Rubin
Guest





PostPosted: Mon Sep 29, 2003 3:46 pm    Post subject: Re: Free memory problem in STL List Reply with quote



Gianni Mariani wrote:
Quote:

Kathy wrote:
...

After I compiled the program and run the EXE file, I found that the program
cannot free the used memory after calling list::clear() function. Also, the
used memory kept alive until the program quit.

That is the expected behaviour.

Can you elaborate on this? My STL documentation says that clear() is
equivalent to erase() over an iterator range, and erase() "destroys the
elements in the range...and removes them [from the list]." How can you
infer from this that memory used to create the list elements is not
supposed to be freed?

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown

Back to top
Gianni Mariani
Guest





PostPosted: Mon Sep 29, 2003 5:34 pm    Post subject: Re: Free memory problem in STL List [OT] Reply with quote

David Rubin wrote:
Quote:
Gianni Mariani wrote:

Kathy wrote:
...

After I compiled the program and run the EXE file, I found that the program
cannot free the used memory after calling list::clear() function. Also, the
used memory kept alive until the program quit.

That is the expected behaviour.


Can you elaborate on this? My STL documentation says that clear() is
equivalent to erase() over an iterator range, and erase() "destroys the
elements in the range...and removes them [from the list]." How can you
infer from this that memory used to create the list elements is not
supposed to be freed?

There are 2 meanings to "free" here.

a) The application marks the resource as being unused

b) The application allows other applications to use the resource

By default, when you "free" (erase, clear, delete etc) you're invoking
behaviour a).

Alot more work is required to invoke behaviour b).

This has nothing to do with C++. I suggest posting questions to
comp.programming.



Back to top
David Rubin
Guest





PostPosted: Mon Sep 29, 2003 11:56 pm    Post subject: Re: Free memory problem in STL List [OT] Reply with quote

Gianni Mariani wrote:
Quote:
David Rubin wrote:

Gianni Mariani wrote:

Kathy wrote:
...

After I compiled the program and run the EXE file, I found that the
program
cannot free the used memory after calling list::clear() function.
Also, the
used memory kept alive until the program quit.


That is the expected behaviour.



Can you elaborate on this? My STL documentation says that clear() is
equivalent to erase() over an iterator range, and erase() "destroys the
elements in the range...and removes them [from the list]." How can you
infer from this that memory used to create the list elements is not
supposed to be freed?


There are 2 meanings to "free" here.

a) The application marks the resource as being unused

b) The application allows other applications to use the resource

This seems to have more to do with returning the resource to the resource pool
(e.g., malloc arena) than allowing other *applications* to use the resource.
But, I think I see where you are going with this. I guess the point is that the
OS's view of the application might show that the memory footprint does not
decrease whereas the application is internally consistent wrt dynamic memory use
(no leaks). And that is OT.

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.