I was reading this question that just got asked: Avoid memory leaks in callbacks?
And I was quite confused, until someone answered the following:
"The problem with this approach is you cannot have a listener which is only referenced in the collection as it will disappear randomly (on the next GC)"
Am I correct in my understanding that using a weak references, like when stored in a WeakHashMap, is incompatible with anonymous listeners?
I typically pass listeners like this:
public static void main(String[] args) {
final Observable obs = new SomeObservable();
obs.addObserver(new Observer() {
public void update(final Observable o, final Object arg) {
System.out.println("Notified");
}
});
obs.notifyObservers();
... // program continues its life here
}
private static final class SomeObservable extends Observable {
@Override
public void addObserver(final Observer o) {
super.addObserver(o);
setChanged(); // shouldn't be done from here (unrelated to the question)
}
}
And I keep track of the listeners using a CopyOnWriteArrayList (the default Observable above apparently uses an old Vector but its just an example to show how I typically create an anonymous class to use as a listener).
As a bonus question: when would the reference to the anonymous listener be eligible for GC should the observable subject use a WeakHashMap? When the main method exits? As soon as the obs.addObserver call is over?
I'm a bit confused about where/how/when references to anonymous class instances are kept/stored/elligible for GC.
Obviously if I'm keeping a normal reference it's not eligible for GC, but what when it's in a WeakHashMap, when does precisely the listener become elligible for GC?