fix(android): prevent memory leak from reused CustomTransition animator#11270
Open
edusperoni wants to merge 1 commit into
Open
fix(android): prevent memory leak from reused CustomTransition animator#11270edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
CustomTransition exposed a single shared AnimatorSet (this.animatorSet)
and returned it from onAppear/onDisappear. androidx clones the transition
per run but shares this field, and the framework attaches its own end
listeners (Visibility$OverlayListener, etc.) to the returned AnimatorSet.
For zero-duration ("none") navigation those listeners are not removed, so
a reused CustomTransition accumulated one stale listener per navigation,
each retaining the previous fragment and its page view. With animations
disabled this leaked the entire navigation history; the leaked Java views
in turn kept their linked JS objects alive forever.
Run on a per-invocation clone of the AnimatorSet so framework listeners
land on a throwaway instance and the shared template stays clean, and
release the animated view (mView) when the animation ends.
|
View your CI Pipeline Execution ↗ for commit 0482482
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Checklist
What is the current behavior?
On Android, navigating forward/back with animations disabled leaks every visited page.
CustomTransitionexposes a single sharedAnimatorSet(this.animatorSet) and returns it fromonAppear/onDisappear. androidx clones the transition for each run but shares that field, and the framework attaches its own end listeners (Visibility$OverlayListener, etc.) to the returnedAnimatorSet. For zero-duration ("none") navigation those listeners are not removed, so a reusedCustomTransitionaccumulates one stale listener per navigation — each retaining the previous fragment and its page view. The leaked Java views in turn keep their linked JS objects alive, so the entire navigation history is held in memory forever (the chain roots through the live fragment'sFragment$AnimationInfo→CustomTransition→AnimatorSet→ stale listeners → previous fragments).With animations enabled the animator actually runs and ends, androidx removes those listeners, and nothing leaks — which is why the leak only reproduces with animations off.
What is the new behavior?
CustomTransitionnow runs each transition on a per-invocation clone of theAnimatorSetinstead of the shared instance, so the framework's end listeners land on a throwaway set and the shared template stays clean and collectable after the transition. It also releases the animated view reference (mView) when the animation ends. Pages and fragments are now garbage-collected after navigation regardless of whether animations are enabled.