1.x: proposal: onTerminateDetach - detach upstream/downstream for GC #3888
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
By default, operators have final link to their child Subscriber and they never clear the upstream's Producer. If the end subscriber is referenced, for example in a
CompositeSubscription
, that keeps an entire chain of objects alive and can cause memory leaks (a use case common on Android).This proposed operator detaches the structures and nulls out references if the sequence terminates or the downstream unsubscribes - at the cost of atomic operations and mandatory volatile read for each onNext().
I know this issue has been brought up several times, but instead of adding the overhead to every operator, I propose an operator that can be applied when the developer really needs it (i.e., could be part of the usual compose(subscribeOn/observeOn) setup).
On the implementation side, this requires deferred
Producer
/request()
handling because requests from downstream may appear even before the upstream callssetProducer
(if at all) and we can't use the base class' behavior for this.