Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

iOS PopModalAsync causes memory leaks #21453

Open
era-maui opened this issue Mar 26, 2024 · 6 comments
Open

iOS PopModalAsync causes memory leaks #21453

era-maui opened this issue Mar 26, 2024 · 6 comments
Assignees
Labels
area-controls-modal legacy-area-perf Startup / Runtime performance memory-leak 💦 Memory usage grows / objects live forever migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/iOS 🍎 s/triaged Issue has been reviewed t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.)
Milestone

Comments

@era-maui
Copy link

Description

Whne you push a modal page that is wrapped in a NavigationPage
Navigation.PushModalAsync(new NavigationPage(new ModularPage())); on iOS we can see that the Page is never released from the memory. We are using Perfiew for debugging.

Here is a GCDump where we open and close a modal page multiple times
20240326_110245_0.zip

I am adding a sample which has both solutions outcommented. Seems counterintuitive that we need to call DisconnectHandler on existing pages and controls when popping a page from the Navigation stack.

NavigationSample.zip

Steps to Reproduce

  1. Open sample app
  2. Start profiling
  3. Press the "Click me" button
  4. Press "Close" button
  5. Repeat steps 3 and 4
  6. MAke a memory GCdump

Link to public reproduction project repository

No response

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

Seems that this can only be fixed by running disconnectHanler on each element on the Page including the page

page.Handler?.DisconnectHandler();

MyLayout.Handler?.DisconnectHandler();

Handler?.DisconnectHandler();

or creating a custom handler for the NavigationPage and running

ViewController.Dispose();
var viewController = ViewController as PageViewController;
viewController.CurrentView = null;

Relevant log output

No response

@era-maui era-maui added the t/bug Something isn't working label Mar 26, 2024
@jsuarezruiz jsuarezruiz added platform/iOS 🍎 legacy-area-perf Startup / Runtime performance labels Mar 26, 2024
@PureWeen PureWeen added the memory-leak 💦 Memory usage grows / objects live forever label Mar 26, 2024
@samhouts samhouts added the migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert label Mar 28, 2024
@AdamEssenmacher
Copy link

Possible duplicate or related:

#20094
#20119

@PureWeen PureWeen added this to the .NET 8 SR6 milestone Apr 25, 2024
@Zhanglirong-Winnie Zhanglirong-Winnie added the s/triaged Issue has been reviewed label Apr 28, 2024
@Eilon Eilon added the t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) label May 10, 2024
@mattleibow
Copy link
Member

/similarissues

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.

@samhouts samhouts added the partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with label May 30, 2024
@PureWeen
Copy link
Member

Can you test with the latest nightly build?
https://github.com/dotnet/maui/wiki/Nightly-Builds

@brentpbc
Copy link

brentpbc commented Jun 4, 2024

I just tried with 8.0.60-ci.net8.24303.1 which seems to be the latest on https://dev.azure.com/xamarin/public/_artifacts/feed/maui-nightly

image

But creating a modal page with a NavigationPage still stops the page from being collected by the garbage collector on a real iOS device. A plain modal with no NavigationPage does get cleaned up after GC Collect.

GC.Collect();
GC.WaitForPendingFinalizers(); 

image

See attached test app. To test tap one of the page buttons, then navigate back and tap the Collect GC button. The isAlive should return to false and the text should change from red to black if it has been successfully cleaned up by the garbage collector. The destructors on the BasePage and BaseViewModel classes should also emit a Debug.Writeline() to the console when they are cleaned up.

MemoryTestApp.zip

@jonathanpeppers
Copy link
Member

NavigationPage has circular references, which would be unrelated to modals:

You can just do this to see it:

// The original NavigationPage & children leak
Application.Current.MainPage = new NavigationPage(new Page1());
Application.Current.MainPage = new Page2();

@PureWeen PureWeen modified the milestones: .NET 8 SR6, .NET 8 SR7 Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-modal legacy-area-perf Startup / Runtime performance memory-leak 💦 Memory usage grows / objects live forever migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner/cat 😻 this is an issue that impacts one of our partners or a customer our advisory team is engaged with platform/iOS 🍎 s/triaged Issue has been reviewed t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.)
Projects
Status: Todo
Development

No branches or pull requests

10 participants