Java(25) _垃圾回收机制

public class Test { 
    //下面的方法会在垃圾回收前调用(对象被回收前)
    /**
     * 这个方法是重写了这个方法,系统本来就有的,垃圾被回收,方法就会被调用,这个方法会在垃圾被回收前调用
     */
    public void finalize() {
        System.out.println("对象即将被回收");
    }
}
                                                                                 

public class examp01 {
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        //创建对象
      Test t1= new Test();
      Test t2=new Test();
       //让对象为垃圾
        t1 = null;
        t2 = null;
        //调用方法回收垃圾
        System.gc();
        Thread.sleep(5000);
    }
}
原文地址:https://www.cnblogs.com/sunnybowen/p/9860209.html