This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

Wesley Liu
Greenhorn

Joined: Dec 12, 2000
Posts: 7
If object obj1 is accessible from object obj2 and object2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection.

True or false question, can anyone give me some thoughts?
Thanks in advance
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Poor wording in the question.
The JLS uses the word "accessible" in a COMPLETELY different manner than this (meaning public/private etc), so this causes a person to start wondering if it is a trick question. Not good.
RE-worded:
If object obj1 has a reference to object obj2 and object2 has a reference to obj1, then obj1 and obj2 are not eligible for garbage collection.
True. But it is not the easiest thing in the world to do to create this situation without creating a circular reference error.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Here's an example of this situation:

This doesn't create a circular reference, but I wonder if this sort of code is considered poor from a design standpoint.
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
I believe the answer to this question is false - the objects ARE eligible for garbage collection.
Mughal states (answer to question 8.1)
"[...]Circular references do not prevent objects from being garbage collected. An object is not eligible for garbage collection as long as the object can be reached from the active part of the program."
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi

If object obj1 has a reference to object obj2 and object2 has a reference to obj1, then obj1 and obj2 are not eligible for garbage collection.

well i agree with Cindy ... the question is worded very poorly ... and this makes it more difficult to answer the question

my view is the that the answer would be FALSE<\b> if both the references obj1 and obj2 are set to null .. but since that has not been specified in the question .... i guess the answer would be TRUE<\b>
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
See this example:
class ObjA {Object b;}
public class Test
{
public static void main(String args[])
{
ObjA a = new ObjA();
a.b = new Object();
a = null;
}
}
An object is eligible for GC when there is no reference to it. Whether it has reference to other objects is not of concern.
Therefore, object 'a' above has a reference to an Object object, but it is still eligible for GC. (However, the Object object is referenced by 'a' until 'a' is garbage collected).
Now comes the example of circular reference:
class Obj1{Obj2 o2;}
class Obj2{Obj1 o1;}
public class Test
{
public static void main(String args[])
{
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();
obj1.o2 = obj2;
obj2.o1 = obj1;
obj1 = null;
obj2 = null;
}
}
I don't know how Sun developers tackle this issue. You can argue that both of them are still referenced. For me, since there is no outside reference to these two objects, it's no longer possible to munipulate them anymore. Leave these 2 objects in memory just leads to memory leak.
As such, it is logical for the answer to the above question to be FALSE.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The question really doesn't say that there are not other references to the objects, but . . . that is back to the poorly written question topic.
The way the question is written right now the answer is MAYBE. You can't say that it is false, because that would mean that they ARE eligible for gc EVEN IF there are other references to them .
If we add in Kevins condition (where the original variables are set to null) then the objects are indeed unreachable and the answer is false.
Per the JLS:

If the Java virtual machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never
again become reachable (I).

Which is pretty much what Mughal said.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC
 
Similar Threads
Circular references and garbage collection
Jdiscuss question of the day - April 3, 2005
About GC
garbage Collection
Question about garbage collection