aspose file tools*
The moose likes Beginning Java and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection " Watch "Garbage Collection " New topic
Author

Garbage Collection

Wei Lien Chun
Greenhorn

Joined: Mar 18, 2004
Posts: 5


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

Joined: Nov 05, 2003
Posts: 98
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


"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection