File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Question" Watch "Garbage Collection Question" New topic

Garbage Collection Question

Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 108
I have a question on "isolation a Reference" discussed in kathy and Bert' book on Garbage Collection

The code example:

My question is

After i4.i = i2; i3, i4 and i2 are formed as Island Objects so they are already eligible for garbage collection, is that right? So why they have to be nulled to be garbage collected?
Vivek Bansal

Joined: Dec 15, 2004
Posts: 1
The nature of automatic garbage collection has an important consequence:
You can still get memory leaks. If you allow live, accessible references to
unneeded objects to persist in your programs, then those objects cannot be
garbage collected. Therefore, it may be a good idea to explicitly assign null into a variable when you have finished with it. This issue is particularly noticeable if you are implementing a collection of some kind.

Thanks<br />Vivek Bansal
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 108
Thanks for your reply. Here I am refering to the idea of islands of isolation. So if Island objects are formed, they are elibible for garbage collection.

1. I understand assigning null is a good practice. But is there a difference as to assigning null to them and not assigning null to them?

2. If asked after which line of code, i3, i4 and i2 will be eligible for garbage collection, can I say after the line "i4.i = i2;"?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The answer is that as long as the objects are accessable from your main() method using i2, i3, or i4, the objects cannot be garbage collected. If you can reach one object, you can reach them all because they form a ring of references.

So you must set i2, i3, and i4 all to null before any of the three objects can be garbage collected.

Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
'can I say after the line "i4.i = i2;"?'

No. If you can picture it, at this point you have a ring of three joined together kites each with a string attached to your hand. As Mike explained, you must let go of all those strings for the kites to be blown away. Even though they stay connected to each other.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 108
Thanks a lot! I am clear.
I agree. Here's the link:
subject: Garbage Collection Question
jQuery in Action, 3rd edition