Cyclic object graph question

L

Laird Nelson

In many object oriented designs, the model calls for something like this:

+---+1 1+---+
| A +-----+ B |
+---+ +---+

....i.e. an A object has a reference to a B object, and that B object has
a reference to the A object.

Suppose the only reference to this little hairball is to A, e.g. suppose
that A is put into a container like a web session or a hashtable or
something like that.

Now suppose that /that/ reference is set to null, i.e. the A-B graph is
removed from the hashtable or session, and there is nothing else that
has a reference to that graph. In my understanding, this means that the
A-B graph is not reachable, and hence is eligible for cleaning up by the
garbage collector, even though B will never release its reference to A,
and A will never release its reference to B.

So: /will/ this situation prevent the garbage collector from reclaiming
the memory occupied by the A-B graph? My colleague says yes; I say no.
Where could I go to find out more information on this?

Thanks,
Laird
 
C

Chris Smith

Laird said:
In many object oriented designs, the model calls for something like this:

+---+1 1+---+
| A +-----+ B |
+---+ +---+

...i.e. an A object has a reference to a B object, and that B object has
a reference to the A object.

Suppose the only reference to this little hairball is to A, e.g. suppose
that A is put into a container like a web session or a hashtable or
something like that.

Now suppose that /that/ reference is set to null, i.e. the A-B graph is
removed from the hashtable or session, and there is nothing else that
has a reference to that graph. In my understanding, this means that the
A-B graph is not reachable, and hence is eligible for cleaning up by the
garbage collector, even though B will never release its reference to A,
and A will never release its reference to B.

So: /will/ this situation prevent the garbage collector from reclaiming
the memory occupied by the A-B graph? My colleague says yes; I say no.
Where could I go to find out more information on this?

The object graph you're referring to is not reachable, so yes it is
eligible for collection according to the rules of the language.

As for whether it actually *will* be collected, there is technically no
answer, since the Java VM specification never specifically requires any
particular capabilities of the garbage collector, aside from asserting
that there will be one. All else is left up to QOI issues. However,
I'm not aware of there EVER having been a compliant Java virtual machine
that would fail to collect these. Certain, no virtual machine provided
by Sun has ever had that particular shortcoming.

I tend to think it's excessive paranoia to worry about collection of
cycles, and to write code to break cycles circumvents all of the
benefits of garbage collection, only to ensure that your code doesn't
run out of memory when running with an extremely poor, substandard
garbage collector.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top