为什么要配置()一个超出范围的java.awt.Window? [英] Why should you have to dispose() a java.awt.Window that goes out of scope?

查看:114
本文介绍了为什么要配置()一个超出范围的java.awt.Window?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的应用程序中发现的一个内存泄漏是 java.awt.Window.allWindows private static field,它会跟踪每个实例化的窗口。我们有创建,使用,然后被遗忘的对话框,并且期望的是这些对话框将被消除并被垃圾回收。这个私有字段保持范围,无限期地,直到调用 dispose()方法。根据定义,我们不能在超出范围的情况下执行此操作。

One of the memory leaks I've discovered in our application is the java.awt.Window.allWindows private static field, which keeps track of every Window instantiated. We have dialog boxes that are created, used, and then forgotten, and the expectation was that these would go away and be garbage collected. This private field keeps them in scope, indefinitely, until the dispose() method is called on them. And by definition, we can't do that when they've gone out of scope.

我不明白为什么这样设计。这似乎与垃圾收集的精神相反,必须明确让系统知道什么时候我完成了一个Window对象。很明显,我已经完成了,因为它超出了范围。

I don't understand why this is designed this way. It seems contrary to the spirit of garbage collection to have to explicitly let the system know when I'm done with a Window object. Obviously I'm done with it, as it is out of scope.

我明白了什么是 dispose()方法是:摆脱系统对等体对象。我明白这是Java之外的,你需要一些方法来做到这一点,而且Swing不应该只是失去对这些对象的跟踪,否则会有内存泄漏。但是,当我永远不会再使用它时,通过保持对我的窗口的引用来实现什么?

I understand what the dispose() method is doing: getting rid of system peer objects. I do understand that this is outside of Java and that you need some way to do that and that Swing shouldn't just lose track of those objects, or else it would have a memory leak. But what is accomplished by keeping a reference to my Window around forever, when I am never going to use it again?

有人可以解释为什么这是必要的吗? >

Can someone explain why this is necessary?

推荐答案

我讨厌说,但这只是GUI的工作原理。

I hate to say it, but that's just how a GUI works.

Windows是非阻塞的。意思是一旦你在代码中创建一个代码,你的代码就会继续执行。

Windows are non-blocking. Meaning that once you create one in code, your code continues to execute.

这意味着你的窗口可能在创建之后立即超出范围,除非你明确地存储一个引用到别的地方此时窗口仍然在屏幕上。

This means that your Window probably goes out of scope immediately after creation, unless you explicitly stored a reference to it somewhere else. The Window is still on screen at this point.

这也意味着,当您完成它时,您还需要一些其他方式来摆脱它。输入 窗口dispose() 方法,可以从Window的一个监听器中调用它。

This also means you need some other way to get rid of it when you're done with it. Enter the Window dispose() method, which can be called from within one of the Window's listeners.

这篇关于为什么要配置()一个超出范围的java.awt.Window?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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