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
- tap on the button “Go To Non-Shell Page” to navigate away from the shell page.
- 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.
- Notice how our shell and our shell page destructors never get called; nothing outputted to the console.
- 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.
- Now that we are on the shell page, press “GC Collect and output memory used”
- 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)
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.
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
Releasebuild of the app with-p:_BundlerDebug=true:This includes the optional Mono diagnostic component in the app. You could also try a
Debugbuild, but if you are measuring performance someDebug-mode things might get in the way.Next, we’ll need to use
mlaunchto launch the simulator and set an environment variable,$DOTNET_DiagnosticPorts.Find
mlaunch:I picked the newest one.
Then set an
aliasfor it, I put this in my~/.zprofileas well:Now you can just run
mlaunchlater.Find a simulator to use:
I just picked iPhone 15, we’ll need the GUID here in the
mlaunchcommand later.Run
dotnet-dsrouter:Take note of the
DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,listenand the PID.Run
mlaunchusing the udid of the simulator:I used
nosuspend, so the app wouldn’t pause on launch.suspendis only useful if you are measuring startup.Once the app is running, you can do either one of:
The
*.gcdumpfiles can only be opened on Windows in Visual Studio or PerfView, so I normally drop those on a shared OneDrive.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
If you only have one
AppShellinstance 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 theAppShellin a variable to be reused later if needed.