...
This noncompliant code snippet is an example of unintentional object retention and is commonly called the Lapsed Listener. The button
continues to hold a reference of the reader
object even after completion of the readSomething
method. ThusAs a result, the garbage collector will not collect the reader
object. A similar problem occurs with inner classes as they hold an implicit reference to the outer class.
...
The solution is to use the finally
block in order to ensure that the reader
object's reference is unregistered.
...
A referent is the object that is being referred to. As soon as any strong references to the object are found to have phased out, the garbage collector reclaims the referent. With WeakHashMap
, the map's key is weakly referred to and thus as a result determines whether the corresponding referents are ready to be collected.
...
Note that the two-argument constructor of WeakReference
takes a Queue
argument and has to be used in order to perform direct queue processing.
...