Effective Java says:
A third common source of memory leaks
is listeners and other callbacks. If
you implement an API where clients
register callbacks but don’t
deregister them explicitly, they will
accumulate unless you take some
action. The best way to ensure that
callbacks are garbage collected
promptly is to store only weak
references to them, for instance, by
storing them only as keys in a
WeakHashMap.
I am a beginner in Java. Could somebody teach me how to create weak references in callbacks and tell me how they solve the memory leak problems? Thanks.
Read this article
The key take aways are :
followed by the WeakListModel listing which I won’t post to avoid cluttering this response.