如何解决摆动侦听器内存泄漏? [英] How to resolve swing listener memory leaks?

查看:126
本文介绍了如何解决摆动侦听器内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

因此,我读到Swing应用程序中的内存泄漏通常是由于使用各种侦听器(鼠标,键,焦点等)引起的.本质上,由于您将对象注册为侦听器而忘记了注销该对象,因此通知程序最终会保留该对象的引用,并会泄漏一些内存.

So I read that often memory leaks within Swing applications originate from the use of various listeners (mouse, key, focus, etc). Essentially, because you register an object as a listener and forget to deregister the object, the notifier ends up holding onto the reference of the object, and leaks a bit of memory.

我知道我们的应用程序不会取消监听器的注册,并且对潜在的解决方案进行了一些研究:

I knew our application wasn't deregistering listeners and did a bit of research on potential solutions:

我发现处理该问题的一种方法是使用WeakReference,有关摆动侦听器的方法的完整详细信息,请参见

I found one approach in dealing with the problem was the use of a WeakReference, full details on the approach with swing listeners can be found here.

然后,我对 NetBeans 表单编辑器如何生成代码以在将侦听器添加到表单后进行清理感到好奇并发现NetBeans正在通过包装对象(即

I then became curious about how the NetBeans form editor was generating code to clean up after listeners added to the form and discovered that NetBeans was registering listeners via a wrapping object i.e.

argTypeComboBox.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
      argTypeComboBoxItemStateChanged(evt);
    }
});

但是生成的代码似乎从未通过调用removeItemListener清除.

But the generated code did not seem to ever clean up by calling removeItemListener.

问题

包装对象是否像弱引用一样起作用?在我看来,它可能会泄漏少量内存(包装对象的大小)?

Is the wrapping object acting like a weak reference? To me it looks like it could leak a tiny amount of memory (the size of the wrapping object)?

在与侦听器打交道时,您是否有其他方法来确保在完成侦听器后始终将其垃圾回收?

Do you have alternative approaches when dealing with listeners to ensure that they are always garbage collected when you are finished with them?

推荐答案

首先进行更正,这里的潜在泄漏并不小.匿名内部类拥有对外部类的引用,因此只要侦听器可以访问,它将保留整个类.

First a correction, the potential leak here is not tiny. An anonymous inner class holds a reference to the outer class, so as long as the listener is reachable it will hold on to the whole class.

但是,这通常不是问题,因为您正在向框架上的对象添加侦听器.当该框架被处理(但是,重要的是被处理)并且没有更多的引用(这是很典型的)时,它的所有组件都变得无法访问(如果您没有做任何花哨的事情)并且整个事情都被垃圾收集了.

However, this is typically not a problem, as you are adding listeners to objects on a frame. When that frame is disposed (important that it be disposed, though) and has no more references (which is pretty typical), all of its components become unreachable (if you didn't do anything fancy) and the whole thing gets garbage collected.

我曾经处理过一个应用程序,但是确实做了一些花哨的事情,例如用其他窗口注册打开的窗口,因此如果关闭了窗口,它仍然被注册-大量的内存泄漏-这些窗口不是很小

I once dealt with an application, however, that did do fancy things, such as registering open windows with a different window, so if the window was closed, it was still registered - big time memory leak - these windows were not tiny.

因此,最重要的是,NetBeans并没有做任何事情来导致内存泄漏",因为该组件是由框架引用的,而不是在框架之外,并且该组件引用了匿名类,后​​者又引用了框架-放置框架,整个图是无法到达的,但是您必须谨慎对待听众,因为他们可以对您这样做.

So the bottom line is that NetBeans isn't doing anything to cause memory "leaks" here as the component is referenced by the frame and not outside of it and the component references the anonymous class, which in turn references the frame - dispose the frame and the whole graph is unreachable, but you do have to be careful with listeners, as they can do this to you.

这篇关于如何解决摆动侦听器内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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