Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

GC

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic