例如:
public class Test12 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Person();
new Person();
new Person();
System.out.println("the program is ending");
}
}
class Person {
public void finalize() {
System.out.println("the object is going");
}
}
编译运行的结果:
the program is ending 作者: 孙玉伟 时间: 2012-11-27 13:59
这是java中的垃圾回收机制,java中垃圾回收是jvm虚拟机自己调用的,无需像c,c++那样血药程序员自己写回收方法,这样能有效的防止内存溢出的问题,方便了开发。那你首先得知道finalize方法在什么条件下才会触发。类似于C++中的析构函数,虚拟机在回收对象实例时,会调用该对象的finalize方法。System.gc()会回收,并且触发,finalize();