在调用
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
,它是否成为垃圾?
我认为在调用System.gc()
时,a1
、a2
和mas
因为状态为null而可以进行垃圾回收,但是我可能错了。