Effective Java (7) - 避免终止方法

一. 基本概念

1. 所谓的终结方法事实上是指finalize()。

2. Java的垃圾回收机制仅仅负责内存相关清理。其它资源的清理(释放文件、释放DB连接)须要程序猿手动完毕。

3. 调用System.gc() 仅仅是建议JVM运行垃圾回收(GC)。但什么时候运行、是否要运行由JVM决定。

4. 用户能够自己调用对象的finalize方法。可是这样的调用是正常的方法调用,和对象的销毁过程无关。

5. 假设一个类实现了finalize方法,那么当JVM运行垃圾回收的时候,该方法一定会被调用。

public class C {
	public static void main(String[] args) {
		A a = new A();
		a.b = new B();
		a = null;
		System.gc();
	}
}

class A {
	B b;

	public void finalize() {
		System.out.println("method A.finalize at " + System.nanoTime());
	}
}

class B {
	public void finalize() {
		System.out.println("method B.finalize at " + System.nanoTime());
	}
}

二. finalize的运行过程

当对象不可达时。GC会推断该对象是否覆盖了finalize方法,如没有覆盖则直接将其回收,否则,若对象未运行过finalize方法,将其放入F-Queue队列,由一低优先级线程运行该队列中对象的finalize方法。

运行finalize方法完后。GC会再次推断该对象是否可达,若不可达则进行回收。否则对象“复活”。


三. JDK中代码演示样例

为防止用户忘记关闭资源,JDK中FileInputStream类中覆盖了finalize方法:

    /**
     * Ensures that the <code>close</code> method of this file input stream is
     * called when there are no more references to it.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FileInputStream#close()
     */
    protected void finalize() throws IOException {
        if ((fd != null) &&  (fd != FileDescriptor.in)) {
            /* if fd is shared, the references in FileDescriptor
             * will ensure that finalizer is only called when
             * safe to do so. All references using the fd have
             * become unreachable. We can call close()
             */
            close();
        }
    }

四. 何时使用finalize

1. 尽量少用finalize。最好由系统管理,我们能够定义其它的方法来释放非内存资源。

2. 假设一定要用,那么能够參考FileInputStream类。在finalize检查费无论内存资源释放。




版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-09-29 13:21  zfyouxi  阅读(194)  评论(0编辑  收藏  举报