1. Finalize
如果某个对象实现了finalize() 方法,那么在垃圾回收器回收这个对象的时候,必须调用该对象被覆盖的finalize()方法.
public class testAny {
@Test
public void testGCFinalize() throws InterruptedException{
call();
System.gc();
}
private void call(){
GCFinalize f = new GCFinalize();
f.sayHello();
}
class GCFinalize{
void sayHello(){
System.out.println("hello~~");
}
@Override
protected void finalize(){
System.out.println("My last moment~~~");
}
}
输出结果:
hello~~
My last moment~~~ ( invoked by Finalize() )