EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Listeners are not cleaned up immediately when calling EventBus#unregister

Open tterrag1098 opened this issue 5 years ago • 3 comments

ListenerList#forceRebuild simply sets a boolean to true, and relies on the next event post to do the actual cleanup:

https://github.com/MinecraftForge/EventBus/blob/master/src/main/java/net/minecraftforge/eventbus/ListenerList.java#L223-L231

This means that if unregister is called and the event is never fired again (such as for startup events) the event listener will not be released from memory.

Additionally, there's no way to do this manually even though getListeners is public, since it requires the bus ID which is not exposed.

tterrag1098 avatar Jun 09 '20 23:06 tterrag1098

Doing otherwise can result in a race condition sadly.

cpw avatar Jul 26 '20 20:07 cpw

Could we at least expose a way to do it manually in situations where it will definitely be safe?

tterrag1098 avatar Jul 26 '20 21:07 tterrag1098

As a work around, you can use Java WeakReferences to avoid the listeners preventing large objects from being garbage collected. Check out the PR linked above for an example.

ViRb3 avatar Nov 10 '20 00:11 ViRb3