are there deadly embraces in garbage collection?

J

Jack Fox

I create a dictionary object "A" which contains many objects of class "B".
Each "B" also contains a reference (property) to its parent of class "A".

My question: when I either set "A" to a new occurence or set it to NULL, is
garbage collection going to take care of the former "A" and all of its "B"
objects, or do I need to do something with IDisposable? This is a rather
simple example, is it possible to construct more complicated scenarios of
cross-referencing that will inhibit garbage collection?
 
D

David Browne

Jack Fox said:
I create a dictionary object "A" which contains many objects of class "B".
Each "B" also contains a reference (property) to its parent of class "A".

My question: when I either set "A" to a new occurence or set it to NULL, is
garbage collection going to take care of the former "A" and all of its "B"
objects, or do I need to do something with IDisposable? This is a rather
simple example, is it possible to construct more complicated scenarios of
cross-referencing that will inhibit garbage collection?
see
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx


The GC constructs a graph of all objects, where each object is a vertex, and
the refrences are the edges.

Then it analyzes this graph to find all objects which are unreachable from
any roots. Those objects will be collected. So your A and all it's B's
will be collected all together on the first collection in which no other
objects hold references to A or any B.

David
 

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