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.16k stars 1.74k forks source link

.Net 8/ 9 Memory leaks #25425

Open Zack-G-I-T opened 5 days ago

Zack-G-I-T commented 5 days ago

Description

We are having some memory issues in our app where the app gets slower after some time and noticed in a gcdump file that all our pages have multiple counts.

We created a dummy project with a second transient page, and added a buton with a method to go back, and call dispose method as well. However in the gcdump there are still multiple counts of this page.

Our question is how are transient pages managed in .NET MAUI? We tested in .net 8 and 9 Transient View/ViewModels are not cleaned up.

How do static variables affect memory management in .NET MAUI? If I am injecting a Singleton Service into a ViewModel will this be cleaned up or also the service has to be transient?

This is a basic default project with no viewmodels and minimal dependency injection, so what is causing the page to be kept in memory?

Image

Steps to Reproduce

  1. Open referenced repo
  2. Create a gcdump file after opening and closing the second page
  3. See multiple counts of Second Page

Link to public reproduction project repository

https://github.com/Zack-G-I-T/MauiMemoryIssue

Version with bug

9.0.0-rc.2.24503.2

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

similar-issues-ai[bot] commented 5 days ago

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

gabsamples6 commented 4 days ago

Hi we have the same issues and despite what we do we have memory leaks - I would expect my pages marked as transient to be cleaned up. Any advice? @jonathanpeppers , is this because of https://github.com/dotnet/maui/issues/8538.

jonathanpeppers commented 4 days ago

Are you also using IDisposable on those transient pages?

I am not a fan of DI, in general, so I might not be best to comment about its usage. 😄

gabsamples6 commented 4 days ago

@jonathanpeppers Yes I am using DI and I am using Idisposable on those transient pages but they get not cleaned up either.

jonathanpeppers commented 4 days ago

I think the current behavior is that transient IDisposable won't go away, but @PureWeen might know more details.

PureWeen commented 3 days ago

Yea, that's how IDisposable works by default with MSFT dependency Injection.

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposal-of-services

dotnet-policy-service[bot] commented 3 days ago

Hi @Zack-G-I-T. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.