-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[android] fix memory leak with CarouselView
#18584
Merged
Merged
Conversation
This file contains 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
Fixes: dotnet#17726 Adding `CarouselView` to a page and navigating away from it keeps the page alive forever on Android. I was able to reproduce this in a test. It took me a while to actually track this one down, but the problem boils down to `MauiCarouselRecyclerView` doing: _carouselViewLayoutListener = new CarouselViewwOnGlobalLayoutListener(); _carouselViewLayoutListener.LayoutReady += LayoutReady; ViewTreeObserver.AddOnGlobalLayoutListener(_carouselViewLayoutListener); The `ViewTreeObserver` lives longer than the page, and so: * `ViewTreeObserver` -> `CarouselViewwOnGlobalLayoutListener` * `event LayoutReady` -> `MauiCarouselRecyclerView` * `MauiCarouselRecyclerView` -> `CarouselView` * `CarouselView` -> `Parent.Parent.Parent.*` -> `Page` Thus the `Page` lives forever. If we remove the `LayoutReady` event, instead: * `CarouselViewwOnGlobalLayoutListener` saves `MauiCarouselRecyclerView` in a weak reference. * It can just call `LayoutReady()` directly. * `MauiCarouselRecyclerView` is able to be GC'd now. * `MauiCarouselRecyclerView.Dispose()` already appropriately will call `ClearLayoutListener()` * Lastly, `ViewTreeObserver?.RemoveOnGlobalLayoutListener(_carouselViewLayoutListener)` With these changes, the test passes and I don't think anything will leak now. :) Other changes: * I fixed the typo in `CarouselViewwOnGlobalLayoutListener`. * I sorted the type names in the test alphabetically.
jonathanpeppers
commented
Nov 7, 2023
{ | ||
public EventHandler<EventArgs> LayoutReady; | ||
public void OnGlobalLayout() | ||
class CarouselViewOnGlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this a nested private class, so it could access the LayoutReady
private method.
jonathanpeppers
added
memory-leak 💦
Memory usage grows / objects live forever (sub: perf)
platform/android 🤖
labels
Nov 7, 2023
Eilon
added
the
area-controls-collectionview
CollectionView, CarouselView, IndicatorView
label
Nov 8, 2023
rmarinho
approved these changes
Nov 8, 2023
21 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
area-controls-collectionview
CollectionView, CarouselView, IndicatorView
fixed-in-8.0.6
Look for this fix in 8.0.6 SR1!
memory-leak 💦
Memory usage grows / objects live forever (sub: perf)
platform/android 🤖
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.
Fixes: #17726
Related: #18365
Adding
CarouselView
to a page and navigating away from it keeps the page alive forever on Android. I was able to reproduce this in a test.It took me a while to actually track this one down, but the problem boils down to
MauiCarouselRecyclerView
doing:The
ViewTreeObserver
lives longer than the page, and so:ViewTreeObserver
->CarouselViewwOnGlobalLayoutListener
event LayoutReady
->MauiCarouselRecyclerView
MauiCarouselRecyclerView
->CarouselView
CarouselView
->Parent.Parent.Parent.*
->Page
Thus the
Page
lives forever.If we remove the
LayoutReady
event, instead:CarouselViewwOnGlobalLayoutListener
savesMauiCarouselRecyclerView
in a weak reference.It can just call
LayoutReady()
directly.MauiCarouselRecyclerView
is able to be GC'd now.MauiCarouselRecyclerView.Dispose()
already appropriately will callClearLayoutListener()
Lastly,
ViewTreeObserver?.RemoveOnGlobalLayoutListener(_carouselViewLayoutListener)
With these changes, the test passes and I don't think anything will leak now. :)
Other changes:
CarouselViewwOnGlobalLayoutListener
.