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检查费内存资源是否释放。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值