为什么下面的代码永远不会垃圾收集JDialog实例? 实例X没有参考,并且对话框已被处理。JDialog永远不会垃圾收集
public class Test {
public static void main(String[] args) throws Throwable {
test();
Runtime.getRuntime().gc();
}
public static void test() throws Throwable {
X x = new X();
x.setVisible(true);
x.dispose();
}
public static class X extends JDialog {
public X() {
super();
}
@Override
protected void finalize() throws Throwable {
System.out.println("destroyed !");
super.finalize();
}
}
}
谢谢
只是一般性的建议:你既不能期望'gc()'执行垃圾回收,也不能'调用'finalize()。你不应该依赖'gc()',这是没有必要的。你永远不应该永远不要依赖'finalize()'。 – musiKk 2011-05-04 12:49:31