基本原理及方法示例
public class TestGC01 {
static Map
<String,Object> objectPool
=new HashMap<>();
public static void main(String
[] args
) throws InterruptedException
{
Point p1
=new Point(10,20);
objectPool
.put("point", p1
);
p1
=null
;
objectPool
.clear();
List
<byte[]> list
=new ArrayList<>();
for(int i
=0;i
<100000;i
++) {
list
.add(new byte[1024*1024]);
}
}
}
构建一个实例对象,并通过P1引用指向这个对象 对于p1引用的这个对象何时会被标识垃圾对象,何时会被回收,如何确定此对象被回收了?
1)当p1引用不再指向构建的Point对象时,此对象会被GC系统认为是垃圾对象。
2)当JVM系统触发了GC操作时,对象可能会被回收。
3)此对象会被JVM中的垃圾回收系统(GC系统)进行回收。(释放内存)
触发GC操作(GC系统触发时会对内存中的对象进行可达性分析,就是检测是否还可以通过引用
访问到此对象,假如不能通过任何引用访问此对象,这个对象就会被标识垃圾)
通过JVM参数检测是否触发了GC操作:-XX:+PrintGCDetails