java gc 垃圾回收机制

//创建一个类
public class Person {
    //final   finally   finalize   的区别
    //当一个对象被虚拟机宣告死亡时会先调用finalize()方法(临终感言)
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("person被销毁了");
    }
}
//打印一下
public class Demo09 {
    public static void main(String[] args) {
        new Person();
        new Person();
        new Person();
        new Person();
        new Person();
        new Person();
        System.gc();//都回收了,但不一定能看到几个
    }
}

终止:exit

//运行
public class Demo08 {
    //gc垃圾回收机制:回收堆里的数据l
    public static void main(String[] args) {
        for(int i=0;i<100;i++){
            if(i==50){
                System.exit(0);//终止当前java虚拟机
            }else{
                System.out.println(i);
            }
        }
    
    }
}

猜你喜欢

转载自www.cnblogs.com/l1314/p/12095189.html