CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.18k stars 372 forks source link

[BUG] Changing MediaElement.Source causes memory leak #2019

Closed dolmakalem closed 1 week ago

dolmakalem commented 1 month ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

Simply changing source of a MediaElement increases memory usage of the application constantly.

Expected Behavior

If I change source of the MediaElement, MediaElement should dispose previous data so there is no constant increase in memory usage. In reproduction project, I even used remote file so there shouldn't be any increase in memory at all. Of course it may increase while loading the video but when I load the same video, memory should decrease because MediaElement should dispose previous data of the video.

I found this problem when I was trying to create a simple app that plays hundreds of small audio files one by one. In that app, memory usage increases slowly until it fills the memory of phone. I also want to note, memory increase is much higher in Android. For example, on windows, one change of source increases memory by around 3-4 mb, on Android, it increases it by around 10 mb.

Steps To Reproduce

  1. Open and run the solution
  2. Press the "click me" button
  3. Observe memory usage

Link to public reproduction project repository

https://github.com/dolmakalem/mauimediaelementmemorybug

Environment

- .NET MAUI CommunityToolkit:CommunityToolkit.Maui.MediaElement 4.0.1
- OS: Windows 10
- .NET MAUI: 8.0.40

Anything else?

Hopefully reproduction repo is fine, I don't do these kind of things normally.

dolmakalem commented 1 month ago

This problem doesn't exist in MediaElement 3.1.0.

What I do in my main app is, creating a timer, changing source of the mediaelement in that timer. This works on Windows but on Android, everytime I change the source, memory usage keeps getting higher around 10 mb.

dolmakalem commented 1 month ago

I also realized another interesting thing. I deployed same code to my phone with both MediaElement 4.0.1 and 3.1. Only 4.0.1 had memory issue and in "Services" page under Android settings, I could see 4.0.1 keeps creating a service and it gets deleted after a while. In 3.1 though, that service isn't created. So both versions have different type of background audioplay I think and that may be the cause of problem.

tomasfil commented 2 weeks ago

Also playing short sound and then immediately changing the source and playing another short sound results into CommunityToolkit.Maui.Media.Services ForegroundServiceDidNotStartInTimeException and crashes the app. I guess that might be linked to 4.0.1 creating the service every time.

The 3.1.0 version does not crash