• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Wei Lien Chun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Answer:
g) None of the above

1)I want to know those instances of the class that are eligible for the garbage collection (mine answer is all the four).

2)And also if u know of any good stuff on the gc that is available on the net then do tell me

thanks!

[ May 25, 2004: Message edited by: Wei Lien Chun ]

[ edited to add line breaks -ds ]
[ May 25, 2004: Message edited by: Dirk Schreckmann ]
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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);


i1=null;
i2=null;
i3=null;
i4=null;


}
public static void main (String[] args) {
new J().m1();
}
}


only when u explicitly specify i1=i2=i3=i4=null as in the code above would the objects be eligible for garbage collection.

Preetham
SCJP 1.4
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)I want to know those instances of the class that are eligible for the garbage collection (mine answer is all the four).

After the method m1 returns, all four objects are eligible for garbage collection.

only when u explicitly specify i1=i2=i3=i4=null as in the code above would the objects be eligible for garbage collection.

That's incorrect. The references i1, i2, i3 and i4 all "die", or cease to exist, when the method m1 returns. Since no live references to the four I-type objects then exist, the four I-type objects are all eligible for garbage collection.
[ May 25, 2004: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic