给你个例子:public class TestSome extends Thread { public static void main(String[] args) { TA ta = new TA(); // 生成一个对象 ta = null; // 抛弃一个对象 System.gc(); // 调用垃圾收集
} }class TA { public TA() { } protected void finalize() throws Throwable { super.finalize(); System.out.println("GC is running...."); } }
可是我无法看到finalize() 执行的结果
我想在finalize()中作一些clearup的工作,而不用我"主动"调用该方法
finalize()方法是在GC认为需要的时候去调用。
你主动调用不会产生任何效果。
TA ta = new TA(); // 生成一个对象
ta = null; // 抛弃一个对象
System.gc(); // 调用垃圾收集
}
}class TA {
public TA() { } protected void finalize() throws Throwable {
super.finalize();
System.out.println("GC is running....");
}
}