dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

Memory leak in animations #25001

Open owl-guy opened 1 month ago

owl-guy commented 1 month ago

Description

When creating an animation that repeats, new TweenerAnimation Objects are created continuously, filling up the app's memory. If you leave it going long enough, the app will crash.

The provided sample repo includes two memory dumps, one was taken after the app was started, the other one after a few minutes. A diff comparison shows an increase in TweenerAnimation objects: image

Steps to Reproduce

  1. Create a new .NET MAUI Project
  2. Commit an animation that repeats itself (let repeat function be () => true)
  3. Start the app with profiler enabled
  4. Take a memory dump
  5. Leave the app running for a few minutes
  6. Take another memory dump and compare with the first one. The number of TweenerAnimation objects will have increased a lot

Link to public reproduction project repository

https://github.com/owl-guy/PerfTest

Version with bug

8.0.91 SR9.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

jfversluis commented 1 month ago

@jonathanpeppers FYI

jonathanpeppers commented 1 month ago

I remember this happening for me in a production app in Xamarin.Forms, circa 2016 as well. But I can't find an issue on this.

We took the approach:

With this in place, does the problem go away?

owl-guy commented 1 month ago

@jonathanpeppers i will try this out, but the problem is that we have views in our app that act as display screens, which means the user will stay in that view for days or even weeks while it's being animated. Your suggestion will not help for this case.

jonathanpeppers commented 1 month ago

@owl-guy if you have a more appropriate place to call CancellationTokenSource.Cancel(), do that. If the animation is not currently on the screen, you should cancel it.

owl-guy commented 1 month ago

@jonathanpeppers i understand, but our problem arises because the animation is on the screen for a long time. If you leave the view with the animation open long enough, the app will crash because it's running out of memory

Alex-111 commented 1 month ago

It seems to be on all platforms. Just tested with Windows....

Zhanglirong-Winnie commented 1 month ago

This issue has been verified using Visual Studio 17.12.0 Preview 2.1(8.0.82 & 8.0.91 & 8.0.90). Can repro this issue on windows, android and ios platforms.