Weak References to Event Listeners #7
Labels
No Label
1
13
2
21
3
34
5
55
8
bug
core
could have
duplicate
enhancement
help wanted
must have
proc
question
should have
wont have
L
M
S
XL
bug
bugfix
discussion
documentation
feature
maintenance
postponed
refactoring
wontfix
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: zdm/event-bus#7
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Currently, if the GC would otherwise garbage collect an object, it currently can't as it will still be referenced from event bus.
I doubt that every user (i.e. us) will dereference an object in
EventBus
when it should otherwise be GC'ed.Any reference to the foreign object should be referenced using
WeakReference
or something similar.[Bug] Only listen weakly on objects to avoid memory leaksto Only listen weakly on objects to avoid memory leaksFor that reason,
EventBus#removeListener
exists. If we no longer need a listener, we remove it and then it's available for garbage collection.If, however, a dedicated event listener is defined (nothing special, there definitely are use cases for that), it isn't necessarily referenced from anywhere outside
EventBus
. Using a weak reference here would cause the listener to be garbage collected and thus fail to handle incoming events.Do you see a solution for this? We could introduce some kind of parameter for this, as in
EventBus#registerWeakly
, but admittedly that is quite verbose.No.
How often have we used removeListener until now? Never.
Do you actually think that will be any different for almost everyone else?
I doubt so. It's much more wanted that objects that should be gced actually get gced.
WeakReference
contains the method get which returns the object or null if it has been gced. By simply checking that we can implement it.This only proves that the need for garbage collection is actually negligibe as most event listeners have the same lifetime as the entire application.
If a listener with a restricted lifetime exists, it can be unregistered, although it's unlikely that this will happen often, as you just mentioned.
But what about dedicated listeners? Most of those would arguably have application lifetime, so garbage collection wouldn't be an issue in the first place. However, with weak references such a listener would get GC'd almost immediately after creation and thus the user would be forced to keep an external reference to it that wouldn't have been necessary otherwise.
Well that was once.
Because then we used actual
EventListener
s.But the average lifespan of any object is lower than that of an actual
EventListener
.As we now allow any object, and as we now are trying to open eventbus to more people (maven), I think changing this behavior should be implemented.
Alright, so as discussed I will make weak references the default for event listeners, while adding an annotation which enforces a regular reference.
Only listen weakly on objects to avoid memory leaksto Weak References to Event Listeners