【发布时间】:2015-08-30 15:45:17
【问题描述】:
在调用System.gc() 时哪些对象可用于垃圾回收,为什么?
public class GCTest {
static class A {
private String myName;
public A(String myName) {
this.myName = myName;
}
}
public static void main(String[] args) {
A a1 = new A("a1");
A a2 = new A("a2");
ArrayList list = new ArrayList();
list.add(a1);
A[] mas = new A[2];
mas[0] = a2;
a2 = a1;
clear(mas);
a1 = null;
a2 = null;
System.gc();
// some code
...
}
private static void clear(A[] mas) {
mas = null;
}
}
如果object == null 会变成垃圾吗?
我认为 a1、a2 和 mas 在调用 System.gc() 时可用于垃圾收集,因为它声明为 null。还是我错了?
【问题讨论】: