[android] fix memory leak in TabbedPage - #21218
Merged
Merged
Conversation
Fixes: dotnet#17959 Context: https://github.com/VoznichkaSviatoslav1/TabbedPageMemoryLeak In the sample app, I added some `GC.Collect()` calls to force the GC to run, but the following log message never prints: public partial class MyTabbedPage : TabbedPage { public MyTabbedPage() { InitializeComponent(); Console.WriteLine("Constructor was called"); } // Doesn't print!!! ~MyTabbedPage() => Console.WriteLine("Destructor was called"); I took a memory snapshot with `dotnet-gcdump` and found a tree like: SampleApp.MyTabbedPage -> Microsoft.Maui.Controls.Platform.MultiPageFragmentStateAdapter<Microsoft.Maui.Controls.Page> -> Action<Microsoft.Maui.Controls.Platform.AdapterItemKey> I didn't find an explanation of what was holding `MultiPageFragmentStateAdapter`. I tried eliminating the `Action<T>`, but that didn't solve the underlying issue; it just made the object tree shorter: SampleApp.MyTabbedPage -> Microsoft.Maui.Controls.Platform.MultiPageFragmentStateAdapter<Microsoft.Maui.Controls.Page> I could also reproduce the problem in a new device test. Reviewing the code, I found we were doing: _viewPager.Adapter = new MultiPageFragmentStateAdapter<Page>(tabbedPage, FragmentManager, _context) { CountOverride = tabbedPage.Children.Count }; But then in the cleanup code, we never set `Adapter` to `null`: _viewPager.Adapter = null; After this change, the memory is released as expected. I am not exactly sure *why* this leaked, but the change does seem appropriate and fixes the problem. Now the new device test passes & the sample app prints: 03-14 15:06:32.394 6055 6055 I DOTNET : Constructor was called 03-14 15:06:37.008 6055 6089 I DOTNET : Destructor was called
PureWeen
previously approved these changes
Mar 14, 2024
Member
Author
Contributor
Member
Author
I spent 2.5 hours on this, and did not figure it out! Comment for now as original issue is Android.
Member
Author
|
I disabled the test on Windows for now, since the original issue is Android. I spent 2 hours commenting out code, and can't seem to figure it out. I don't understand what the |
Member
Author
|
WindowsAppSDK looks like it makes extensive use of Probably worth me learning about it, might be useful for other things. |
PureWeen
approved these changes
Mar 16, 2024
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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: #17959
Context: https://github.com/VoznichkaSviatoslav1/TabbedPageMemoryLeak
In the sample app, I added some
GC.Collect()calls to force the GC to run, but the following log message never prints:I took a memory snapshot with
dotnet-gcdumpand found a tree like:I didn't find an explanation of what was holding
MultiPageFragmentStateAdapter. I tried eliminating theAction<T>, but that didn't solve the underlying issue; it just made the object tree shorter:I could also reproduce the problem in a new device test.
Reviewing the code, I found we were doing:
But then in the cleanup code, we never set
Adaptertonull:After this change, the memory is released as expected. I am not exactly sure why this leaked, but the change does seem appropriate and fixes the problem.
Now the new device test passes & the sample app prints: