garbage collector for JFrame#39;s object(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 对象的垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFrame 对象的垃圾收集器
基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
