public class Exec {
public static void main(String[] args) {
Book b1 = new Book();
System.out.println("当前有" + Book.counter + "个对象");
Book b2 = new Book();
System.out.println("当前有" + Book.counter + "个对象");
Book b3 = new Book();
System.out.println("当前有" + Book.counter + "个对象");
b3 = null;
System.gc();
System.runFinalization();
System.out.println("当前有" + Book.counter + "个对象");
b2 = null;
System.gc();
System.runFinalization();
System.out.println("当前有" + Book.counter + "个对象");
}
}
class Book {
public static int counter = 0;
public Book() {
++counter;
}
public void finalize() {
--counter;
}
}
静态成员使用回收
最新推荐文章于 2023-07-08 15:17:48 发布