JFrame 对象的垃圾收集器
import javax.swing.*;
public class Main
{
public Main()
{
JFrame jf = new JFrame("Demo");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(100, 100);
jf.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
Runtime.getRuntime().gc();
}
}
我调用 Runtime.getRuntime().gc();
来显式调用垃圾收集器.但是窗口并没有从屏幕上消失,为什么垃圾收集器不回收JFrame的对象?
I call Runtime.getRuntime().gc();
for explicit garbage collector invoking. But window doesn't dissapear from screen, why doesn't garbage collector reclaim JFrame's object?
推荐答案
当一个 JFrame
被创建时,它会在一些内部的 Swing 数据结构中注册自己,从而允许它接收像鼠标点击这样的事件.这意味着在您告诉 Swing 使用 dispose()
摆脱窗口之前,有一个对潜伏在某处的对象的引用.
When a JFrame
is created, it registers itself in some internal Swing data structures which allow it to receive events like mouse clicks. This means there is a reference to your object lurking somewhere until you tell Swing to get rid of the window using dispose()
.
相关文章