adding elements to vector and deleting them later does not free memory 
Author Message
 adding elements to vector and deleting them later does not free memory

Hello,

I encountered a really strange problem while writing a little chess program
(using VStudio.Net 2002, Win XP).
The program works really fine, except one thing: it does not free the used
memory!
I checked for memory leaks by overwriting my new operator, but every "new"
statement i made within the program has a corresponding "delete" somewhere
else - which is called of course.
Because the program itself has grown I created a simple test case:
I wrote a small program that looks like this:

* Listing *
// create a vector that will hold my objects
std::vector<chessfield *> * myvector = new std::vector<chessfield *>;

// fill and clear the vector 100 times
for (int i = 0; i < 100; i++)
{
    // create 10.000 objects and store them in the vector
    for (int j = 0; j < 10000; j++)
    {
        chessfield * test = new chessfield(0, 0, 0);

        myvector->push_back(test);
    }

    // now delete every object inside the vector
    for (unsigned int k = 0; k < myvector->size(); k++)
        delete myvector->at(k);

    // clear the vector
    myvector->clear();

Quote:
}

delete myvector;

* Listing end *

(chessfield is a class that represents a field on the chessboard)

I ran this test case inside my chess program and watched the task manager:
the result was, that the program allocated memory, but does not free it
properly.
But the stranges thing is what happened, when I copy'n'pasted this little
program into a seperate new project, placed it inside main and let it run:
now the memory is freed properly, using the same classes.

Next I checked for the compiler settings I made, but they were identical in
each projects.

I'm really confused about this and don't know where I've made the mistake.

Any suggestions?

Thanks in advance,

Sven



Mon, 12 Dec 2005 03:31:12 GMT  
 adding elements to vector and deleting them later does not free memory



Quote:
> Hello,

> I encountered a really strange problem while writing a little chess
program
> (using VStudio.Net 2002, Win XP).
> The program works really fine, except one thing: it does not free
the used
> memory!
> I checked for memory leaks by overwriting my new operator, but every
"new"
> statement i made within the program has a corresponding "delete"
somewhere
> else - which is called of course.
> Because the program itself has grown I created a simple test case:
> I wrote a small program that looks like this:

You have written a lot of Java code before, have you not?

In C++ we generally don't have to dynamically allocate all the
objects, the STL is value based.

Quote:

> * Listing *
> // create a vector that will hold my objects
> std::vector<chessfield *> * myvector = new std::vector<chessfield

*>;

std::vector<chessfield> myvector;

Quote:

> // fill and clear the vector 100 times
> for (int i = 0; i < 100; i++)

If i wasn't an int but a user defined (class) type, ++i would be more
efficient.

Quote:
> {
>     // create 10.000 objects and store them in the vector
>     for (int j = 0; j < 10000; j++)
>     {
>         chessfield * test = new chessfield(0, 0, 0);

>         myvector->push_back(test);

You don't have to allocate memory for the objects, let vector do that:

    myvector.push_back(chessfield(0, 0, 0));

or just

    myvector.push_back(chessfield());

if chessfield can be default constructed.

Quote:
>     }

>     // now delete every object inside the vector
>     for (unsigned int k = 0; k < myvector->size(); k++)
>         delete myvector->at(k);

not needed if you store chessfield objects and not pointers.

Quote:

>     // clear the vector
>     myvector->clear();

The vector destructor will do this for you anyway.

Quote:
> }

> delete myvector;

If you don't allocate the vector dynamically, you don't need to delete
it either.

Quote:

> * Listing end *

> Any suggestions?

If you just want to test a vector of 10000 default constructed
chessfields, all you have to do is:

for (int i = 0; i != 100; ++i)
{
    std::vector<chessfield>    myvector(10000);

Quote:
}

> Thanks in advance,

> Sven

Bo Persson



Mon, 12 Dec 2005 04:05:40 GMT  
 adding elements to vector and deleting them later does not free memory

Quote:

>Hello,

>I encountered a really strange problem while writing a little chess program
>(using VStudio.Net 2002, Win XP).
>The program works really fine, except one thing: it does not free the used
>memory!
>I checked for memory leaks by overwriting my new operator, but every "new"
>statement i made within the program has a corresponding "delete" somewhere
>else - which is called of course.
>Because the program itself has grown I created a simple test case:
>I wrote a small program that looks like this:

>* Listing *
>// create a vector that will hold my objects
>std::vector<chessfield *> * myvector = new std::vector<chessfield *>;

>// fill and clear the vector 100 times
>for (int i = 0; i < 100; i++)
>{
>    // create 10.000 objects and store them in the vector
>    for (int j = 0; j < 10000; j++)
>    {
>        chessfield * test = new chessfield(0, 0, 0);

>        myvector->push_back(test);
>    }

>    // now delete every object inside the vector
>    for (unsigned int k = 0; k < myvector->size(); k++)
>        delete myvector->at(k);

>    // clear the vector
>    myvector->clear();
>}

>delete myvector;

>* Listing end *

>(chessfield is a class that represents a field on the chessboard)

>I ran this test case inside my chess program and watched the task manager:
>the result was, that the program allocated memory, but does not free it
>properly.

It frees it properly, and the memory will be available for subsequent
allocations by your program, subject to whatever fragmentation has occurred
in your program's heap. It just doesn't shrink its address space, and the
main drawback to that is the OS may end up swapping pages which your app is
no longer (actively) using. You can avoid that in some cases by using the
VirtualAlloc family of functions (which aren't suited for use with
std::vector).

Quote:
>But the stranges thing is what happened, when I copy'n'pasted this little
>program into a seperate new project, placed it inside main and let it run:
>now the memory is freed properly, using the same classes.

>Next I checked for the compiler settings I made, but they were identical in
>each projects.

>I'm really confused about this and don't know where I've made the mistake.

To detect memory leaks, don't use Task Manager. See the MSDN documentation,
starting here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsde...

P.S. As Bo Persson points out, this fragment could be rewritten not to use
dynamic allocation on your end, but your question is still a good one.

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 12 Dec 2005 04:17:22 GMT  
 adding elements to vector and deleting them later does not free memory
First of all, thanks for your help!

Got me Bo, yes, I've been working with Java for the last 7 month, shame on
me (but I needed the money *g*) :)

Despite the fact that I could let the vector class handle the memory
allocation/deallocation,
I still find this is weird behaviour, because memory that I freed via delete
SHOULD be really
free, but as Doug pointed out, the first thing I thought of is memory
defragmentation;
the question thats coming next to my mind is: is this a problem with the
underlying OS (WinXP) or with my program?
And the next big question: why does it work in the simple test program, but
not in my main project? Identical code behaving different in two projects,
thats really odd I think?

Sven



Quote:
> Hello,

> I encountered a really strange problem while writing a little chess
program
> (using VStudio.Net 2002, Win XP).
> The program works really fine, except one thing: it does not free the used
> memory!
> I checked for memory leaks by overwriting my new operator, but every "new"
> statement i made within the program has a corresponding "delete" somewhere
> else - which is called of course.
> Because the program itself has grown I created a simple test case:
> I wrote a small program that looks like this:

> * Listing *
> // create a vector that will hold my objects
> std::vector<chessfield *> * myvector = new std::vector<chessfield *>;

> // fill and clear the vector 100 times
> for (int i = 0; i < 100; i++)
> {
>     // create 10.000 objects and store them in the vector
>     for (int j = 0; j < 10000; j++)
>     {
>         chessfield * test = new chessfield(0, 0, 0);

>         myvector->push_back(test);
>     }

>     // now delete every object inside the vector
>     for (unsigned int k = 0; k < myvector->size(); k++)
>         delete myvector->at(k);

>     // clear the vector
>     myvector->clear();
> }

> delete myvector;

> * Listing end *

> (chessfield is a class that represents a field on the chessboard)

> I ran this test case inside my chess program and watched the task manager:
> the result was, that the program allocated memory, but does not free it
> properly.
> But the stranges thing is what happened, when I copy'n'pasted this little
> program into a seperate new project, placed it inside main and let it run:
> now the memory is freed properly, using the same classes.

> Next I checked for the compiler settings I made, but they were identical
in
> each projects.

> I'm really confused about this and don't know where I've made the mistake.

> Any suggestions?

> Thanks in advance,

> Sven



Mon, 12 Dec 2005 05:24:25 GMT  
 adding elements to vector and deleting them later does not free memory

Quote:
> Despite the fact that I could let the vector class handle the memory
> allocation/deallocation,
> I still find this is weird behaviour, because memory that I freed via
delete
> SHOULD be really
> free, but as Doug pointed out, the first thing I thought of is memory
> defragmentation;
> the question thats coming next to my mind is: is this a problem with the
> underlying OS (WinXP) or with my program?

Depends on what you mean by "freed". It is expensive to keep calling the OS
for memory so typically it is managed by a heap manager and the heap manager
sub-allocates out of the heap it manages. That is faster. Ultimately that is
where container allocation, malloc() and new/new[] will get its memory from.
When you deallocate, free(), delete or delete[] the memory is returned to
the heap manager waiting to be recycled for use. It is _not_ returned to the
OS.

So that means something like Task Manager might well reflect the total
amount requested by the heap manager not the amount in use. It could be 6Mb
was requested but only 3Mb is in use, the other 3Mb is "free" memory within
the heap manager waiting to be put in use by the next call to
malloc()/new/new[] etc.

But some heap allocators have a function like _heapshrink() which will
return any free memory it can back to the OS. VC++ 4.0 and earlier did have
such a function but for VC++ 5.0 and later, it does nothing

Stephen Howe



Mon, 12 Dec 2005 08:19:13 GMT  
 adding elements to vector and deleting them later does not free memory
Quote:

> First of all, thanks for your help!

> Got me Bo, yes, I've been working with Java for the last 7 month, shame on
> me (but I needed the money *g*) :)

> Despite the fact that I could let the vector class handle the memory
> allocation/deallocation,
> I still find this is weird behaviour, because memory that I freed via delete
> SHOULD be really
> free,.

It is freed, from the delete / new standpoint, it isn't freed from the
OS standpoint.

Here is what happens : when you first call new (or malloc), the CRT
asks the OS for memory. When you call delete on that same memory, the
CRT doesn't handle it back to the OS. Instead, it keeps this memory so
that it can be reused in a subsequent "new" call without needing to
call the OS.

Quote:
> the question thats coming next to my mind is: is this a problem with the
> underlying OS (WinXP) or with my program?

Between the 2 :-) : it's the CRT free memory caching mechanism. That's
why you shouldn't rely on the task manager (or any other OS level
tool) to check memory consumption.

Quote:
> And the next big question: why does it work in the simple test program, but
> not in my main project?

In the test project, you are leaving the program just after memory
deallocation right? When a process exits, all the memory it allocated
is given back to the OS.

Arnaud
MVP - VC



Mon, 12 Dec 2005 20:57:03 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Deleting element of a vector

2. free memory in vector

3. delete Operator doesnt free Memory ???

4. Memory Allocation and Freeing using New and Delete Operators

5. priority_queue: order of elements with equal priority is not preserved when popping elements

6. vector::pop_back and vector::erase leaking memory?

7. Memory Leak (delete not working?)

8. C# App Does Not Free Up Memory?

9. free() is not releasing memory, help

10. How not to free the memory of a function after execution

11. farmalloc() and farfree() not freeing memory?

12. Malloc not freeing memory

 

 
Powered by phpBB® Forum Software