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 

pls help: deleting ptrs from deque

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Charlie Cosse
Guest





PostPosted: Sun Sep 19, 2004 10:46 am    Post subject: pls help: deleting ptrs from deque Reply with quote



Hi, the following code does not reclaim memory. Can someone please
point out what I'm doing wrong? Thanks!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <iostream>
#include <deque>
using namespace std;

int main(int *argc,char **argv[]){

std::deque<double *> doubles;
int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??
p++;
}

while(true){cout<<"looping to monitoring memory"< return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Charlie Cosse
Guest





PostPosted: Mon Sep 20, 2004 5:54 am    Post subject: Re: pls help: deleting ptrs from deque Reply with quote



[email]ccosse (AT) asymptopia (DOT) com[/email] (Charlie Cosse) wrote in message news:<58329000.0409180930.4278bdf1 (AT) posting (DOT) google.com>...
Quote:
Hi, the following code does not reclaim memory. Can someone please
point out what I'm doing wrong? Thanks!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <iostream
#include using namespace std;

int main(int *argc,char **argv[]){

std::deque int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??
p++;
}

while(true){cout<<"looping to monitoring memory"< return 0;
}



Responding to my own post here, the problem is that, on my machine, at
least,
it is necessary to perform deletions more frequently. There is a plot
of
"time to reclaim memory for a deque (and vector)" vs "number of
elements
in that container" at:
http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp
Myself, i was using the deque as a "garbage can" and saving references
to
objects and numbers throughout the rendering of an animation frame,
then
"emptying the garbage" once per cycle. This led to rapid exhaustion of
memory, even when using a vector. The solution was to empty the
garbage
after every pixel of the animation rendered. Below is another piece
of
code which runs entirely differently than the above, and fixes the
problem.
This is what i would call "pathalogical" behavior, as it seems to be
different between hardware platforms running the same compiler. Here
is
my fix:
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

//This solves the problem of exhausting memory for large deques: in
short, empty the
//deque before it gets too big!!

#include #include <vector>
#include <deque>

using namespace std;

class waste{
public:
waste(){};
void take(double *x){doubles.push_back(x);};
void dump(){
std::vector<double *>::iterator p=doubles.begin();
//cout<<"dumping: "< while(p != doubles.end()){
delete *p;
p++;
}
doubles.clear();
//cout<<"done"< };
int size(){return(doubles.size());};

private:
std::vector };

int main(int *argc,char **argv[]){
waste *w=new waste();
while(1){
double *x=new double(100.);
w->take(x);
if((w->size()%1000)==0){
//cout<<"calling dump: "< w->dump();
}
}

return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Frey
Guest





PostPosted: Mon Sep 20, 2004 5:54 am    Post subject: Re: pls help: deleting ptrs from deque Reply with quote



Charlie Cosse <ccosse (AT) asymptopia (DOT) com> wrote:
Quote:
Hi, the following code does not reclaim memory. Can someone please
point out what I'm doing wrong? Thanks!

It is reclaiming memory, actually. Probably due to memory fragmentation,
the memory is not returned to the OS until the deque<> doubles object
is destroyed.

You can confirm this theory by placing the entire allocate/deallocate
in a loop and doing it repeatedly. On my system, doing it 1,000,000
times, memory usage stayed at approximately 28megs.

Only when I put the deque object in a function of its own did that memory
get released to the OS. Try this example, so you can see it happening
for yourself. This example would likely work with new/deleting the
deque object itself as well, but this was easier.

- Chris

#include <iostream>
#include <deque>
using namespace std;

char buffer[100];

void testdeque() {
deque<double *> doubles;
int counter=0;

cout << "After deque instantiation:" << endl;
cin.getline(buffer, 100);

while(counter<1000000){
doubles.push_front(new double(1000.));
counter++;
}

cout << "After 1,000,000 allocations:" << endl;
cin.getline(buffer, 100);

deque while(p != doubles.end()){
delete *p;
p++;
}

cout << "After 1,000,000 deletes:" << endl;
cin.getline(buffer, 100);

// get rid of the old pointers
doubles.clear();

cout << "After doubles.clear: " << endl;
cin.getline(buffer, 100);
}


int main(int *argc,char **argv[]){

while(true) {
testdeque();
cout << "After function return: " << endl;
cin.getline(buffer, 100);
}

return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 20, 2004 5:55 am    Post subject: Re: pls help: deleting ptrs from deque Reply with quote

Charlie Cosse wrote:
Quote:
Hi, the following code does not reclaim memory. Can someone please
point out what I'm doing wrong? Thanks!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <iostream
#include using namespace std;

int main(int *argc,char **argv[]){

std::deque int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??

That frees the storage for the double, but not the storage for the
pointer to it. Also, strictly speaking, I think you are supposed to
set the pointer to 0 or some other valid pointer value.

Quote:
p++;
}

You could use:

while (!doubles.empty())
{
double *& p = doubles.front();
delete *p;
p = 0;
doubles.pop_front();
}

However, it would be easy to forget to free all the data that the
deque elements point to, particularly in the case that an exception
occurs. If you really want a queue of doubles it would be much simper
to use a deque<double>. If, as I suspect, you actually want to store
larger objects that you want to avoid copying or you want to store
objects of several classes with a common base class, then consider
using a reference-counting smart-pointer such as boost::shared_ptr.

Quote:
while(true){cout<<"looping to monitoring memory"<

If you're trying to check for memory leaks by running top or Task
Manager while this loop runs, don't bother. Those utilities tell you
how much memory is allocated to a process by the operating system.
When a C++ program frees memory it is normally put on a free list
within the process and is not returned to the operating system until
the process ends, so it won't reduce the reported memory consumption.

Quote:
return 0;
}

--
Ben Hutchings
Nothing is ever a complete failure; it can always serve as a bad example.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Sep 20, 2004 3:31 pm    Post subject: Re: pls help: deleting ptrs from deque Reply with quote

In article <58329000.0409180930.4278bdf1 (AT) posting (DOT) google.com>, Charlie
Cosse <ccosse (AT) asymptopia (DOT) com> writes
Quote:
int main(int *argc,char **argv[]){

std::deque<double *> doubles;
int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??
Reclaim what memory? That for the dynamic double, or that for pointer in

the deque? For the latter, see next. The former is dealt with as far as
possible already (note that neither free() nor delete guarantees
returning memory to the OS,)


Quote:
p++;
}

{

std::deque<double *> temp;
doubles.swap(temp);
}
note that placing that in a block ensures that the dtor for temp is
called on exit from the block. That now owns the data blocks that had
been owned by doubles.


Quote:
while(true){cout<<"looping to monitoring memory"< return 0;
}

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephan Br?nnimann
Guest





PostPosted: Mon Sep 20, 2004 6:28 pm    Post subject: Re: pls help: deleting ptrs from deque Reply with quote

[email]ccosse (AT) asymptopia (DOT) com[/email] (Charlie Cosse) wrote in message
news:<58329000.0409180930.4278bdf1 (AT) posting (DOT) google.com>...
Quote:
Hi, the following code does not reclaim memory. Can someone please
point out what I'm doing wrong? Thanks!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <iostream
#include using namespace std;

int main(int *argc,char **argv[]){

std::deque int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

check virtual and resident memory of the process here!

Quote:

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??
p++;
}

What memory do you want to "reclaim"? The one that (did) hold the double
or the one used by the deque node? For the latter you'll also need to
erase `p' instead of increasing the iterator.

p = doubles.erase(p);

However: AFAIK the standard does not define how a deque should handle
the memory, see e.g. http://groups.google.com/groups?&th=1584efcc06db92c1
for one of the discussions.

Quote:

while(true){cout<<"looping to monitoring memory"<

Hint: consider using std::cin::get().

Quote:
return 0;
}

You should also take note that UNIX operating systems usually decrease
the memory of a process only if needed: put your test in an endless
loop to check if memory is really available after clean-up of the deque.

regards,
Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Sep 20, 2004 7:10 pm    Post subject: Re: pls help: deleting ptrs from deque Reply with quote

[email]ccosse (AT) asymptopia (DOT) com[/email] (Charlie Cosse) wrote in message
news:<58329000.0409180930.4278bdf1 (AT) posting (DOT) google.com>...

Quote:
Hi, the following code does not reclaim memory.

What exactly do you mean by "reclaim memory"? Which memory isn't being
reclaimed?

Quote:
Can someone please point out what I'm doing wrong?

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <iostream
#include using namespace std;

int main(int *argc,char **argv[]){

std::deque int counter=0;

while(counter<10000000){
doubles.push_front(new double(1000.));
counter++;
}

std::deque while(p != doubles.end()){
delete *p; //***PROBLEM*** how can i reclaim memory??
p++;
}

while(true){cout<<"looping to monitoring memory"< return 0;
}

How do you determine that memory hasn't been "reclaimed"?

There are two immediate issues that I can imagine:

- Typically, once memory is allocated to a process, it remains
allocated to that process. Delete returns the memory to the process
specific free pool, and deleted memory can be reused by the process,
but if you are monitoring memory be looking at some external measure
(the size of the process, as reported by the task manager or top,
for example), you won't see this.

- You've not done anything to change the size of the deque, so if you
are using a tool which displays how much memory is actually
allocated (from the new/malloc point of view), then all of the
memory used by the deque will still be allocated. (That's a minimum
of 40MB on most systems.)

In the first case, there's not much you can do about it, at least at the
C++ level. (You could arrange to allocate memory at some lower level,
and then free it.) In the second, the memory will be recovered when the
deque goes out of scope, or you can force its recovery earlier with
something like:

deque< double* >().swap( doubles ) ;

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

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