About Garbage Collection

C

C# Learner

<code>

public class MainClass
{
private ArrayList list;

public Class()
{
list = new ArrayList();

AddToList();
ReadFromList();
}

private void AddToList()
{
list.Add(new RecordClass("test1"));
list.Add(new RecordClass("test2"));
list.Add(new RecordClass("test3"));
}

private void ReadFromList()
{
if (((RecordClass)list[0]).Field == "test1") {
// do something
}

if (((RecordClass)list[1]).Field == "test2") {
// do something
}

if (((RecordClass)list[2]).Field == "test3") {
// do something
}
}
}

public class RecordClass
{
private string field;

public RecordClass(string field)
{
this.field = field;
}

public string Field
{
get { return field; }
set { field = value; }
}
}

</code>

Now, when i read the list in ReadFromList(), are the list items
*guaranteed* to reference valid RecordClass instances, or would the
garbage collector free the memory that these references should be
pointing to?

I really can't understand how the GC would work in this case. If the
answer to the above question is "yes", then does the GC know to free
up the memory if I were to call ArrayList.Delete()?

Of course, if the answer to the first question is "no", then I'd have
to use a struct instead of a class for RecordClass, right -- since
it's a value type?

TIA
 
N

Nicholas Paldino [.NET/C# MVP]

C# Learner,

See inline:

C# Learner said:
Now, when i read the list in ReadFromList(), are the list items
*guaranteed* to reference valid RecordClass instances, or would the
garbage collector free the memory that these references should be
pointing to?

The list items are guaranteed to be there. This is because the
ArrayList is holding references to the items (they are reference types) and
your class instance is holding a reference to the ArrayList. Because of
this, the items are reachable, and not subject to garbage collection. Now,
if your class instance was to be let go, and not reachable, and the other
references are not reachable, then the ArrayList (and the items in the list,
if references are not stored anywhere else that is reachable) will be
eligible for GC>
I really can't understand how the GC would work in this case. If the
answer to the above question is "yes", then does the GC know to free
up the memory if I were to call ArrayList.Delete()?

When you call Delete on the ArrayList, the ArrayList removes the item
from the list, letting go of its reference. If there are no other
references out there, then the item is eligible for GC (when the runtime
determines that a GC is needed)

Hope this helps.
 
M

Morten Wennevik

Yes, they will be valid. Your program holds a reference to the arraylist,
and the arraylist holds references to the recordclass objects. The
garbage collector will search for the parent reference of any object and
as long as the topmost reference is still valid, all the other references
will also be valid.

There is no ArrayList.Delete(), but if you set

list = null;

the program will not hold any reference to the arraylist object anymore.
The object will be flagged for termination as well as all the contents of
it, since once the arraylist is gone, there is no valid reference to the
recordclasses either.

Also, if you call list.Clear(); the references to the recordclass objects
will be lost and the objects will be collected.
The ArrayList object will still be valid since list still holds the
reference to it.
 

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