Memory leaks - After running 30 minutes it crashes .NET 9 with API 27 - not fixed since .NET 8
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 297
Description
### Description
In reference to this request in this thread: [https://github.com/dotnet/maui/issues/17049#issuecomment-2598567614](https://github.com/dotnet/maui/issues/17049#issuecomment-2598567614)
I have a simple app, a blank template that reads data from the accelerometer. I noticed that after approximately 20 minutes with the app running, the smartphone completely crashes, becoming completely unresponsive, requiring a hard reset by pressing the power button for 8 seconds.
So I used a basic loop in MauiProgram.cs to measure RAM usage and found that over time, memory usage increases at a rate of approximately 250 kb/s with the accelerometer reader running and 50 kb/s with just the blank home page open with nothing running in the background.
It only happened when I tested it on a physical Motorola Moto E5 Play device with 2 GB of RAM, the application process starts consuming 300mb of memory and increases eternally until it reaches 1GB and crashes the device due to lack of memory. Testing on a Xiaomi MI 9 with Android 11 - API 30 and 6GB of RAM, the phenomenon does not happen, RAM consumption is static and there is no accumulation, the same with Android 15 on an Android Emulator.
```
private static async Task Diag()
{
while (true)
{
long memoriaUsada = Process.GetCurrentProcess().PrivateMemorySize64;
long memoriaTrabalhada = Process.GetCurrentProcess().WorkingSet64;
Console.WriteLine($"Used memory (Private): {memoriaUsada / (1024 * 1024)} MB");
Console.WriteLine($"Workload memory (Working Set): {memoriaTrabalhada / (1024 * 1024)} MB");
await Task.Delay(1000);
}
}
```
Code to read data from accelerometer:
```
public void StartAccelerometer()
{
try
{
if (Accelerometer.Default.IsSupported)
{
SensorSpeed _sensorSpeed;
Accelerometer.Default.ReadingChanged += Accelerometer_ReadingChanged;
Accelerometer.Default.Start(SensorSpeed.Game);
}
}
catch (FeatureNotSupportedException)
{
....
}
}
private void Accelerometer_ReadingChanged(object? sender, AccelerometerChangedEventArgs e)
{
_latestData = e.Reading;
}
```
**Edit: The video was sped up 16x demonstrating the memory load increasing over 20 minutes.**

### Steps to Reproduce
1. Check if .NET 9 is installed
2. Create a blank .MAUI project in Visual Studio 2022 with .NET 9
3. Implement the above mentioned functions in MauiProgram.cs
4. Install SDK and USB driver via Android SDK manager
5. Get a physical device or emulator running Android 8.1 Oreo
6. Compile and run the application on the device and check whether the RAM usage sample is increasing over time in the output log.
### Link to public reproduction project repository
_No response_
### Version with bug
9.0.50 SR5
### Is this a regression from previous behavior?
No, this is something new
### Last version that worked well
_No response_
### Affected platforms
Android
### Affected platform versions
Android 8.1 Oreo - API 27, VS 17.14.36121.58
### Did you find any workaround?
**No solution found.**
### Relevant log output
```shell
```
Contributor guide
Assessment
This issue has not been assessed yet.