-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Description
After extensive testing and investigation, I believe there is a memory leak in Maui when using Android foreground services. The issue happens when a foreground service is started, the app is swiped away, and then the app is restarted. This causes certain classes to be re-instantiated without ever being cleaned up. However, the issue seems to be highly related to how viewmodels and pages are registered in MauiProgram.cs. My current configuration registers the app's home page and associated viewmodel as singletons, and every other page and viewmodel as transient. This causes every page and viewmodel besides the home page to be recreated every time the app is swiped and restarted while a foreground service is running. Including AppShell.
Here is a snapshot of AppShell in my app after swiping and opening the app 9 times while a foreground service is running:

And another of my top level pages:

If there is a simple way to handle this in Maui, then all advice is welcome.
Another case to consider here is if you have a slow API call in the app while a foreground service is running. Then the user swipes away the app because "nothing" is happening. Then the API call completes, but the activity no longer exists. What should happen?
Link to my repro repo: https://github.com/KillswitchPrime/MauiFGSKeepsAppAlive
Steps to Reproduce
Check README in the repro repo.
I mostly followed the steps in this link:
https://github.com/dotnet/maui/wiki/Memory-Leaks
Link to public reproduction project repository
https://github.com/KillswitchPrime/MauiFGSKeepsAppAlive
Version with bug
Tested on 9.0.307.
Likely affects most/all other versions.
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
No response
Affected platforms
Android
Affected platform versions
Android 10 and up
Did you find any workaround?
No response
Relevant log output
See screenshots further up.
