w*********n 发帖数: 439 | 1 class Beta { ... }
class Alpha {
static Beta b1;
Beta b2;
}
public class Tester {
public static void main (String [ ] args) {
Beta b1 = new Beta();
Beta b2 = new Beta();
Alpha a1 = new Alpha();
Alpha a2 = new Alpha();
a1.b1 = b1;
a1.b2 = b1;
a2.b2 = b2;
a1 = null;
b1 = null;
b2 = null;
}
}
问题:在上面的a1, a2, b1, b2中哪几个对象会被gc回收? | b******y 发帖数: 9224 | 2 我试着回答一下,不一定对,仅作参考:
Since Alpha.b1 is class level variable, a1.b1 = b1, so we have a2.b1 = b1.
a1.b1 = b1;
a1.b2 = b1;
a2.b1 = b1;
a2.b2 = b2;
a1=null, and no other objects reference a1, so a1 can be gc'ed
a2 is never set to null, so a2 is still used.
b1 is a class level static variable and still referenced by a2, thus b1 is
stil used.
b2 is referenced by a2, thus b2 is still used.
So, the answer is, only a1 can be 回收。 |
|