Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Memory leaks when replacing Application.Current.MainPage by a new one keep the old page in memory #23034

Closed
Djakar opened this issue Jun 13, 2024 · 2 comments · Fixed by #23489
Labels
area-controls-general General issues that span multiple controls, or common base classes such as View or Element fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@Djakar
Copy link

Djakar commented Jun 13, 2024

Description

I created a new MAUI app from scratch, removed the Shell and created 2 ContentPages named Page1 and Page2.
In Page1, I just placed a button that calls Application.Current.MainPage = new Page2();
In Page2, I started a thread with a serie of GC.Collect();
After memory analysis, I saw that Page1 is kept in memory.

Steps to Reproduce

  1. Create a new MAUI application

  2. Remove the Shell and the page created by the wizard

  3. Create a new content page and name it Page2.

  4. In code behind, in the ctor, start a new thread like this:
    new Thread(async () => { await Task.Delay(5000); GC.Collect(); GC.Collect(); GC.Collect(); }).Start();

  5. Create a new content page an name it Page1

  6. In Page1, add a button with this statement in the click event handler: Application.Current.MainPage = new Page2();

  7. In App.xaml.cs, replace the shell by MainPage = new Page1();

The issue is similar with 8.0.60 SR6

image
image
image
image
image
image
MauiApp1.zip

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

Not yet

Relevant log output

No response

@Djakar Djakar added the t/bug Something isn't working label Jun 13, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@jsuarezruiz jsuarezruiz added memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/windows 🪟 labels Jun 13, 2024
@RoiChen001 RoiChen001 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert labels Jun 14, 2024
@RoiChen001
Copy link

Can repro this issue at Windows platform on the latest 17.11.0 Preview 2.0(8.0.40&8.0.60).

@PureWeen PureWeen added the area-controls-general General issues that span multiple controls, or common base classes such as View or Element label Jun 20, 2024
@samhouts samhouts removed s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 3, 2024
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jul 8, 2024
Fixes: dotnet#23034
Context: https://github.com/user-attachments/files/15817814/MauiApp1.zip

In the above sample app, you can observe a memory leak on Windows
by doing:

    Application.Current.MainPage = new Page1(); // This page lives forever
    Application.Current.MainPage = new Page2();

Reviewing memory snapshots, it appears that the
`VisualDiagnosticsOverlay` class is keeping the `Page1` instance
alive:

    MauiApp1.Page1
    -> Microsoft.Maui.Platform.ContentPanel
        -> Microsoft.Maui.Controls.VisualDiagnostics.VisualDiagnosticsOverlay
            -> Microsoft.Maui.Controls.Window

In this case, the `Window` holds a reference to the
`VisualDiagnosticsOverlay`, and the `Window` is still open.
`VisualDiagnosticsOverlay`'s base class is `WindowOverlay`, which
has a `_platformElement` field holding the `ContentPanel` which
holds the `Page1` instance.

In this case, `_platformElement` is only used for unsubscribing
events, so it feels like we can just change it to a `WeakReference`
to solve the issue.

I was able to write a test for this scenario as well.
@samhouts samhouts added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
area-controls-general General issues that span multiple controls, or common base classes such as View or Element fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants