Search code examples
javaswinggarbage-collectionguavaevent-bus

EventBus and Swing JFrame Garbage Collection?


I am refactoring a Swing application to use Guava's EventBus pretty heavily. The Swing application basically runs a decision process, displays a large collection of results in a JFrame, and then gets disposed once the JFrame is closed. Now it also subscribes to an EventBus to handle user manipulation of the data.

My question is this. I'm worried the EventBus may hold a reference to the JFrame and all of its internal references (including all the model data) once it is closed. Is it enough to add a WindowListener to unsubscribe it from the EventBus so garbage collection can free the data and the underlying model? Or is this something I don't need to worry about?


Solution

  • You need to unregister all components you previously registered as there is no WeakEventBus.

    Note that unregistering an object

    @throws IllegalArgumentException if the object was not previously registered.
    

    unless the object has no @Subscribe methods. The Javadoc is not exact, as both registering and unregistering such objects do nothing at all (and never throw).