Question about proper garbage collection

T

the.newsletter

Hello,

I'm not quite sure if all objects in my code are released properly by
the garbage collector, because under some circumstances (I didn't
figure them out yet) I encounter sometimes a
System.OutOfMemoryException.

It would be nice if someone of you could tell me if all the memory is
freed correctly by the garbage collector in the following short sample.
All I want to do within this code is to build an ArrayList of self
defined objects (from class Folder) and return them.

Please consider this code:

---------- MyFolder.h ------------
public __gc class Folder
{
public:
Byte id __gc[];
String* name;
};
---------- end of MyFolder.h ------


--------- MyFolder.cpp ------------
// constructor:
Folder::Folder()
{
this->id = null;
this->name = "";
}


// somewhere within a test method:
ArrayList* testMethod()
{
ArrayList* myList = new ArrayList();

for(int i=0; i < 1000; i++)
{
tempFolder = new Folder();
tempFolder->id = new Byte __gc[80];
tempFolder->name = "tempName";

for(int j=0; j < 80; j++) // fill this array now
tempFolder->id[j] = xxx;

myList->Add(tempFolder);
}
return myList;
}
--------- end of MyFolder.cpp -------------------


My concrete question is now, if tempFolder is released properly, or if
I have to release it manually or set to null after each and every loop
cycle!?? Or would it be of any advantage to call GC->Collect() after
every loop cycle??


Thank you in advance
for your hints,

TheLetti
 
W

Willy Denoyette [MVP]

Hello,

I'm not quite sure if all objects in my code are released properly by
the garbage collector, because under some circumstances (I didn't
figure them out yet) I encounter sometimes a
System.OutOfMemoryException.

It would be nice if someone of you could tell me if all the memory is
freed correctly by the garbage collector in the following short sample.
All I want to do within this code is to build an ArrayList of self
defined objects (from class Folder) and return them.

Please consider this code:

---------- MyFolder.h ------------
public __gc class Folder
{
public:
Byte id __gc[];
String* name;
};
---------- end of MyFolder.h ------


--------- MyFolder.cpp ------------
// constructor:
Folder::Folder()
{
this->id = null;
this->name = "";
}


// somewhere within a test method:
ArrayList* testMethod()
{
ArrayList* myList = new ArrayList();

for(int i=0; i < 1000; i++)
{
tempFolder = new Folder();
tempFolder->id = new Byte __gc[80];
tempFolder->name = "tempName";

for(int j=0; j < 80; j++) // fill this array now
tempFolder->id[j] = xxx;

myList->Add(tempFolder);
}
return myList;
}
--------- end of MyFolder.cpp -------------------


My concrete question is now, if tempFolder is released properly, or if
I have to release it manually or set to null after each and every loop
cycle!?? Or would it be of any advantage to call GC->Collect() after
every loop cycle??


Thank you in advance
for your hints,

TheLetti

But you are storing the tempFolder reference to the ArrayList, that means
you keep a reference to the Folder instance alive through myList, so there
is nothing to collect.

Willy.
 
T

the.newsletter

Hi Willy,

thanks for your immediate response. That's kind of true. But it doesn't
explain to me why I get this System.OutOfMemoryException sometimes!?

Above code sample was not complete. Maybe the error occurs in here
(short sample):

....
ArrayList* myList = new ArrayList();

for(int i=0; i < 1000; i++)
{
tempFolder = new Folder();
tempFolder->id = new Byte __gc[80]; // allocate 80 bytes
tempFolder->name = "tempName";

for(int j=0; j < 80; j++) // fill this array now
tempFolder->id[j] = xxx;

// now, sometimes I have to allocate more than the 80 bytes above.
// so I do this:
if(condition)
{
tempFolder->id = new Byte __gc[200]; // allocate even more
memory
// fill array again and work with it
}
myList->Add(tempFolder);
}

So I just allocate another buffer without caring about the already
allocated 80 bytes.
The question is, if the 80 bytes allocated before are LOST or if they
are freed during the next garbage collection cycle?


Thanks again in advance,

TheLetti
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top