maui: Shell Page Memory Leak

Description

Shell pages don’t get garbage collected and end up causing a memory leak. In the provided sample project, we have two types of pages, one that’s simply a ContentPage which we will refer to as non-shell, and one that is a Shell, and all we do is switch between them using App.Current.MainPage = new MainPage(); for the non-shell page and App.Current.MainPage = new AppShell(); for the shell one.

We have a destructor for all classes involved, the shell, the shell’s ContentPage and the non-shell page. We simply output to the console when the destructors get called.

When we are on the shell page, the destructor of the non-shell page gets called properly, and hence it is garbage collected, however when we are on the non-shell page, the destructor of the shell page (or its shell) never gets called, and so leaks memory. We can keep switching back and forth between the shell and non-shell page and we will see the memory leaking and the shell destructors never being called.

Steps to Reproduce

  1. tap on the button “Go To Non-Shell Page” to navigate away from the shell page.
  2. Now we are on the non-shell page, press the button “GC Collect and output memory used” this will force GC.Collect() and output the memory used to the console.
  3. Notice how our shell and our shell page destructors never get called; nothing outputted to the console.
  4. Now press on the button “Go To Shell Page” to go the shell page again; this will be a new instance of the shell and its page.
  5. Now that we are on the shell page, press “GC Collect and output memory used”
  6. Notice how our non-shell page’s destructor is called properly.

Link to public reproduction project repository

https://github.com/ESO-ST/WebViewCrashMidLoad/tree/MemoryLeak

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 36 (18 by maintainers)

Most upvoted comments

I got the workaround to function in our app, I had to modify it a bit. The only caveat here is the page the shell is currently on -Shell.CurrentPage-, along with its UI elements, does not get garbage collected, it’s ignored, I’m still investigating that part.

 public void LoadPageAndCleanShell(Page page)
 {
         Shell shell = Shell.Current;
         if (shell != null)
         {
             foreach (ShellItem item in shell.Items)
             {
                 foreach (ShellSection section in item.Items)
                 {
                     section.Items.Clear();
                 }
                 item.Items.Clear();
             }
             shell.Items.Clear();
             shell.Items.Add(new ShellContent() { Content = new ContentPage() });
         }
         App.Current.MainPage = page;
         App.Current.MainPage.Loaded += OnLoaded;

         void OnLoaded(object sender, EventArgs e)
         {
             shell = null;
             App.Current.MainPage.Loaded -= OnLoaded;
         }
 }

Thanks for your continued help, getting dotnet-gcdump working was invaluable here 😃

Here are my steps to use dotnet-gcdump, they are not intuitive. I would expect folks to have trouble!

First, I build a Release build of the app with -p:_BundlerDebug=true:

dotnet build YourApp.csproj -f net8.0-ios -c Release -p:_BundlerDebug=true

This includes the optional Mono diagnostic component in the app. You could also try a Debug build, but if you are measuring performance some Debug-mode things might get in the way.

Next, we’ll need to use mlaunch to launch the simulator and set an environment variable, $DOTNET_DiagnosticPorts.

Find mlaunch:

find /usr/local/share/dotnet/packs | grep -E 'mlaunch$'
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/tools/lib/mlaunch/mlaunch.app/Contents/MacOS/mlaunch

I picked the newest one.

Then set an alias for it, I put this in my ~/.zprofile as well:

alias mlaunch="/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/tools/lib/mlaunch/mlaunch.app/Contents/MacOS/mlaunch"

Now you can just run mlaunch later.

Find a simulator to use:

xcrun simctl list devices
== Devices ==
-- iOS 17.5 --
    iPhone 15 (472C1E4D-B981-4C75-9638-111E9CB15E5D) (Shutdown)

I just picked iPhone 15, we’ll need the GUID here in the mlaunch command later.

Run dotnet-dsrouter:

dotnet-dsrouter ios-sim
How to connect current dotnet-dsrouter pid=26122 with ios simulator and diagnostics tooling.
Start an application on ios simulator with ONE of the following environment variables set:
[Default Tracing]
DOTNET_DiagnosticPorts=127.0.0.1:9000,nosuspend,listen
[Startup Tracing]
DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,listen
Run diagnotic tool connecting application on ios simulator through dotnet-dsrouter pid=26122:
dotnet-trace collect -p 26122
See https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dsrouter for additional details and examples.

Take note of the DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,listen and the PID.

Run mlaunch using the udid of the simulator:

mlaunch --launchsim bin/Release/net8.0-ios/iossimulator-arm64/YourApp.app --device :v2:udid=472C1E4D-B981-4C75-9638-111E9CB15E5D --wait-for-exit --stdout=$(tty) --stderr=$(tty) --argument --connection-mode --argument none '--setenv:DOTNET_DiagnosticPorts=127.0.0.1:9000,nosuspend,listen'

I used nosuspend, so the app wouldn’t pause on launch. suspend is only useful if you are measuring startup.

Once the app is running, you can do either one of:

dotnet-trace collect -p 26122 --format speedscope
dotnet-gcdump collect -p 26122

The *.gcdump files can only be opened on Windows in Visual Studio or PerfView, so I normally drop those on a shared OneDrive.

@ESO-ST as the Carousel crash issue is unrelated to the memory leak and workaround. Please can you raise an issue specific to the crash once you have a repro?

here’s the new ticket for this, with a repro project

@ESO-ST please can you send me the gcdump so I can pass it on?

thanks @jonathanpeppers I was able to get dotnet-gcdump working using image

If you only have one AppShell instance in the entire app, any sub pages should be able to go away just fine.

If you need to change Application.MainPage, you could consider storing the AppShell in a variable to be reused later if needed.