• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

GC

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


I think the object referenced by i4 is not eligible for collection cos it does not form an island of object.The instance variable refers to its own reference varible.But the answer says all are eligble for garbage collection. Is it becos i4 ia a local reference and is cleared off the stack when the method is exited?

Pls clarify
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, here i1,i2,i3,i4 are with in the scope of the method m1. After the execution of the method 'm1', no reference is pointing to the objects created in method 'm1'. Hence all the objects are eligible for garbage collection.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic