JFrame 对象的垃圾收集器 [英] garbage collector for JFrame's object

查看:42
本文介绍了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().

这篇关于JFrame 对象的垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆