dotnet / dotnet/maui

[Android] Suspected memory leak when using android foreground services

Open
#33,018 3 comments 1 reaction 0 assignees View on GitHub
area-controls-general perf/memory-leak 💦 platform/android s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

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:
Image

Here is one of my subpages:
Image

And another of my top level pages:
Image

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.

Contributor guide

Open the contributing guide

Research direction

Start with the README in the linked MauiFGSKeepsApp reproduction and inspect the MauiProgram.cs registrations described in the issue. Compare behavior across Android 10 and later while repeatedly swiping away and restarting the app with a foreground service running. Done requires a confirmed minimal reproduction and a clear conclusion about whether MAUI retains recreated pages or viewmodels after restart.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, csharp
Domain
mobile-dev, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.