blob: d6d642bc6e0cd95b28ec7f1f23195458057b4dcb [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqWeakListener
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqWeakListener
:description: Apache NetBeans wiki DevFaqWeakListener
:toc: left
:toc-title:
:syntax: true
== What is a WeakListener
When you attach a listener to another object, via, for example, an `addPropertyChangeListener()` method, that other object now holds a _reference_ to that listener.
In Java, any object that is referenced by another object which is still in use (i.e. referenced by something else that is still alive, and so forth) cannot be garbage collected. One of the most frequent sources of memory leaks in Swing applications is attaching a listener to some long-lived object and never detaching the listener. The entire object graph of the listener and anything it references is held in memory, whether it is needed or not, until the object being listened to is finally garbage collected.
Since listeners are often implemented as inner (non-static) classes of some other object, and an inner class keeps a reference to the object that created it, the outer object instance is kept in memory too.
`link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/WeakListeners.html[WeakListeners]` is a factory class which wraps your event listener in another one, but it only _weakly_ (using `java.lang.ref.WeakReference`) references your actual listener. That means that, even though you are listening for changes, that will not block your listener from being garbage collected if nothing _else_ still references it.
There is one caveat to using WeakListeners - if you do something like this:
[source,java]
----
someObject.addPropertyChangeListener(WeakListeners.propertyChange(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
...
}
}, someObject);
----
in fact you _are not listening_ on someObject for any amount of time - the anonymous PropertyChangeListener you created will be instantly garbage-collected. So keep a reference to your listener when using WeakListeners.
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqWeakListener[http://wiki.netbeans.org/DevFaqWeakListener] ,
that was last modified by NetBeans user Tboudreau
on 2010-01-24T06:12:27Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.