• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

reg dan's test suite

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is reg the below question from dan's mock exam

class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

Ans and explanation is :

4 g None of the above Please note that this question asks which object is NOT eligible for garbage collection after method m1 returns. The objects referenced by i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1 references any of those objects. When method J.m1 returns, the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects.


I dont understand this. any explantaions please...
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just look at this post:
a very good explanation by Corey is given
GC
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is dealing with the issue of what happens when a bunch of objects all have references to each other, but none of those objects is accessible to any user threads anymore. The answer is that the garbage collector is smart enough to know that "isolated islands" of objects that refer to each other are no longer being used, and they should be considered eligible for garbage collection.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Which object is not eligible for garbage collection after method m1 returns?".

Carefully observe this question ... it is " after method m1() returns " not "after main method completes ".
At this point of time object created by "new J()" still exists which is having references to the island of objects.
So, obviously none of the objects is eligible for garbage collection.

Regards
Jana
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion. Can we say all of the variable is the local variable and object instatiate on the method.

So after method return. All of them will not accessible again and eligible for GC.

Is I am right about the local variable.

Thank.
[ April 26, 2005: Message edited by: Sunyaluk Boonmas ]
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sunyaluk

ya you are right that as the method return all objects created within the methods are eligible for gc unless and untill it returns some object created within the method

Well for others.. carefull see the question its asking about NOT eligbile for gc
here all the objects are eligible for gc even just before the method returns so answer "none of above " means that what objects are not elibible for gc ? the answer is "none of above"
ie all objects are eligible for gc.

:-)
hope i m right !!!
 
Jane W pemberley
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,Amit .. got the point
Thanks
Jane
 
karthik venkatesan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now its clear, but still I4 is referring the same I4. So its not forming the island of objects with I1,I2 or I3 right. then it will not be eligible for garbage collection. right?
 
reply
    Bookmark Topic Watch Topic
  • New Topic