maui icon indicating copy to clipboard operation
maui copied to clipboard

Page-level memory leak on modal navigation on iOS

Open AdamEssenmacher opened this issue 1 year ago • 4 comments

Description

Pages pushed modally on iOS are not garbage collected after being popped, resulting in the modal page leaking.

Page-level leaks are serious, as they prevent child elements from being collected as well.

Steps to Reproduce

  1. Push page modally
  2. Pop modal page
  3. Force GC runs
  4. Observe modal page never collected

Link to public reproduction project repository

https://github.com/AdamEssenmacher/iOSModalLeak.Maui

Version with bug

8.0.6

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS / MacOS

Affected platform versions

iOS 17.2

Did you find any workaround?

No response

Relevant log output

No response

AdamEssenmacher avatar Jan 23 '24 07:01 AdamEssenmacher

Having same issue using Navigation.PushAsync(). When page is popped from the stack, objects on the popped ContentPage are not collected. On iOS, the app is quickly jettisoned due to excessive RAM usage. Seeing the issue on Windows and Mac Catalyst in addition to iOS.

fischberg avatar Jan 24 '24 03:01 fischberg

@fischberg I opened this issue because I have good reason to believe that there is leak in modal navigation itself, specifically on iOS.

It's a more general problem in MAUI apps where would-be small leaks spread through entire pages and make them uncollectible by the GC--but that's not necessarily an issue specific to navigation. It sounds like your problem may fall in to this category.

The MAUI repo has an in-depth discussion on memory leaks on the wiki: https://github.com/dotnet/maui/wiki/Memory-Leaks

I'm personally working on a toolkit that can help detect and mitigate these general-case leaks, while also helping us isolate leaks to the offending component: https://github.com/AdamEssenmacher/MemoryToolkit.Maui

AdamEssenmacher avatar Jan 24 '24 05:01 AdamEssenmacher

Can repro this issue at iOS platform on the latest 17.10 Preview 4(8.0.6&8.0.20).

RoiChen001 avatar Apr 23 '24 08:04 RoiChen001

Just found something else that could very well be related.

If a XAML element has an assigned name (e.g. x:Name="Something") inside a page that's being displayed modally, that element will leak.

I have a hunch this behavior will go away once the modal page leak does. I can check once the fix is in the nightly builds.

AdamEssenmacher avatar May 08 '24 00:05 AdamEssenmacher