编程语言
首页 > 编程语言> > Java 垃圾回收

Java 垃圾回收

作者:互联网

Object的finalize()方法的作用是否于C++的析构函数作用相同?

与C++的析构函数不同,析构函数调用确定,而它的是不确定的

将未被引用的对象放置在F-Queue队列

方法执行随时可能被终止

给予对象最后一次重生的机会

 

 

finalize示例

public class Finalization {

    public static Finalization finalization;

    @Override
    protected void finalize() throws Throwable {
        System.out.println("finalized");
        finalization = this;
    }

    public static void main(String[] args) throws InterruptedException {
        Finalization f = new Finalization();
        System.out.println("First print: " + f);
        f = null;
        System.gc();
        Thread.sleep(1000);
        System.out.println("Second print:" + f);
        System.out.println(f.finalization);

    }
}

  打印结果:

First print: jvm.gc.Finalization@2503dbd3
finalized
Second print:null
jvm.gc.Finalization@2503dbd3

标签:Java,System,回收,println,gc,垃圾,print,Finalization,out
来源: https://www.cnblogs.com/linlf03/p/12180897.html