java垃圾回收机制的使用

public class Test {
    public static void main(String[] args) throws Exception {
        Book b=new Book(true);
        b.checkIn();
        new Book(true);
        System.gc();
    }

}
class Book{
    boolean checkdOut = false;
    Book (boolean checkOut) {
        checkdOut = checkOut;
    }
    
    protected void checkIn() {
        checkdOut = false;
    }
    protected void finalize() {
        if (checkdOut) {
            System.out.println("Error : check Out");
        }
    }
}

在执行回收机制之前会先执行finalize()

原文地址:https://www.cnblogs.com/chenglc/p/7424390.html