Repository navigation
[android] fix memory leak in Window - #16045
Merged
Merged
Conversation
Fixes: dotnet#15972 Context: https://github.com/markusroessler/MauiWindowScopeSample In the above sample, `android.permission.FOREGROUND_SERVICE` is utilized to keep the app running even when terminated by the user. Unfortunately, in this case `Window` & various other objects live forever. When running the sample, the `InstanceCount` grows each time you close & launch the app again: 07-07 16:08:32.281 22562 22562 I DOTNET : ScopedService InstanceCount: 3 07-07 16:08:32.291 22562 22562 I DOTNET : MainWindow InstanceCount: 3 The first issue I noticed was `IWindow.Destroying()` was never called because of this code in `OnDestroy()`: if (!activity.IsFinishing) { var window = activity.GetWindow(); window?.Destroying(); } `IsFinishing` was always `true` from my debugging in this spot. `OnStop()` had the code: // As of Ice Cream Sandwich, Stopped is guaranteed to be called // even when the activity is finishing or being destroyed // We check for finishing and call destroying here if so if (activity.IsFinishing) window?.Destroying(); In this case `IsFinishing` was `false`. I think the solution here is that `OnDestroy()` should attempt to destroy the `Window` in all cases. There isn't a case you would *not* want to destroy it. The only problem being we can now destroy it twice -- and there is no `IWindow.IsDestroyed` value to check. We can simply handle `InvalidOperationException`, as this should be rare. With this change in place, I still saw a leak. Reviewing a `.gcdump` of the app, I saw the object tree: Microsoft.Maui.Controls.Platform.AlertManager.AlertRequestHelper List<Microsoft.Maui.Controls.Platform.AlertManager.AlertRequestHelper> Microsoft.Maui.Controls.Platform.AlertManager MauiWindowScopeSample.MainWindow Microsoft.Maui.Controls.MessagingCenter.MaybeWeakReference Microsoft.Maui.Controls.MessagingCenter.Subscription List<Microsoft.Maui.Controls.MessagingCenter.Subscription> Dictionary<Microsoft.Maui.Controls.MessagingCenter.Sender, List<Microsoft.Maui.Controls.MessagingCenter.Subscription>> Microsoft.Maui.Controls.MessagingCenter Other Roots Where `MessagingCenter` appears to keep `AlertRequestHelper` alive, which keeps the `Window` alive. After these changes, we instead get this every time: 07-07 16:08:38.967 22562 22562 I DOTNET : ScopedService InstanceCount: 2 07-07 16:08:38.978 22562 22562 I DOTNET : MainWindow InstanceCount: 2 07-07 16:08:39.015 22562 22562 I DOTNET : Lifecycle event: OnStart 07-07 16:08:39.072 22562 22562 I DOTNET : Lifecycle event: OnPostCreate 07-07 16:08:39.073 22562 22562 I DOTNET : Lifecycle event: OnResume 07-07 16:08:39.074 22562 22562 I DOTNET : Lifecycle event: OnPostResume 07-07 16:08:39.411 22562 22585 I DOTNET : MainWindow InstanceCount: 1 07-07 16:08:39.414 22562 22585 I DOTNET : ScopedService InstanceCount: 1 The `InstanceCount` returns to 1 as the finalizers run for these types. I have yet to figure out how to write a test for this scenario -- I've only been able to validate this fix manually testing the app.
jonathanpeppers
commented
Jul 7, 2023
jonathanpeppers
commented
Jul 7, 2023
| Destroying?.Invoke(this, EventArgs.Empty); | ||
| OnDestroying(); | ||
|
|
||
| AlertManager.Unsubscribe(); |
Member
Author
There was a problem hiding this comment.
This call unsubscribes from MessagingCenter things on Android.
Redth
approved these changes
Jul 12, 2023
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 #15972
Context: https://github.com/markusroessler/MauiWindowScopeSample
In the above sample,
android.permission.FOREGROUND_SERVICEis utilized to keep the app running even when terminated by the user.Unfortunately, in this case
Window& various other objects live forever. When running the sample, theInstanceCountgrows each time you close & launch the app again:The first issue I noticed was
IWindow.Destroying()was never called because of this code inOnDestroy():IsFinishingwas alwaystruefrom my debugging in this spot.OnStop()had the code:In this case
IsFinishingwasfalse.I think the solution here is that
OnDestroy()should attempt to destroy theWindowin all cases. There isn't a case you would not want to destroy it. The only problem being we can now destroy it twice -- and there is noIWindow.IsDestroyedvalue to check. We can simply handleInvalidOperationException, as this should be rare.With this change in place, I still saw a leak. Reviewing a
.gcdumpof the app, I saw the object tree:Where
MessagingCenterappears to keepAlertRequestHelperalive, which keeps theWindowalive.After these changes, we instead get this every time:
The
InstanceCountreturns to 1 as the finalizers run for these types.I have yet to figure out how to write a test for this scenario -- I've only been able to validate this fix manually testing the app.