Repository navigation
Description
Description
I'm trying to develop LoB app in MAUI, including lot's of CollectionViews and complex master-details with multiple views, data forms and custom controls. It's bumpy road, but by far the most critical issue for me is the amount of memory leaks, which makes the app completely unusable.
I have already reported 2 issues resulting in severe memory leak and there have been many more raised over time by others. Unfortunately they aren't getting attention they deserve in my opinion.
The more I look into this topic, the more puzzled I'm by memory management in MAUI and the more I'm determined that there is something horribly wrong happening inside MAUI. I decided to create another sample to demonstrate this issue and it's significance for production apps. The sample is very simple MVVM app with CollectionView of Items. Clicking on an item will bring you to a detail page.
This bit in DetailPage constructor was added to increase memory allocation of the detail to make the memory leak more obvious (Array.Clear(Bytes) causes array to be iterated and allocated):
Bytes = new byte[1024 * 400000];
Array.Clear(Bytes);
If you keep navigating back and forth from master to its details and watch memory usage, you will see memory raising from 100 MBs to several GBs very quickly. I added button to execute GC.Collect(), but forcing GC never disposes all the resources.

Calling this to manually free some resources doesn't seem to have any effect.
vm.Bytes = null;
vm.Item = null;
BindingContext = null;
I made several observations:
- Memory doesn't always go up upon opening the DetailPage.
- GC sometimes does seem to be able to dispose some resources.
- Using VS memory profiler, I don't see my objects bloating the memory.
- Using VS memory profiler, I see lot's of UI bits and handlers being stuck in the memory.
- There seem to be no single point of root holding these things.
- Seems like xaml bindings might have something to do with it.
The issue is obvious in other samples as well, for example Monkey app by @jamesmontemagno
https://github.com/dotnet-presentations/dotnet-maui-workshop
Note that there is no use of static resources or event subscription anywhere in the sample code.
I observed the issue only in Windows as I have no sufficient way to check memory in other platforms.
Any tips about how to dispose DetailPage with all the related resources would be very welcomed.
Steps to Reproduce
- Load the repository and run the app.
- Repeatedly open details from CollectionView by selecting them and then pop the DetailPage using the button while observing memory usage in ResMon/memory profiler.
- Click on the "Force GC" button to trigger GC - some memory resources might be disposed, but the memory leak persists.
Link to public reproduction project repository
https://github.com/Vroomer/MAUI-master-detail-memory-leak.git
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Windows, I was not able test on other platforms
Affected platform versions
Windows SDK 10.0.22000
Did you find any workaround?
No response
Relevant log output
No response


Activity
8m0 commentedon Dec 13, 2022
Hi, the problem exists since Xamarin. And you can find similar bug reports for MAUI. I think this problem will be moved to "Backlog milestone" as always.
I remember that result can differ from debug and release. So you can test it on release version just to be sure.
angelru commentedon Dec 13, 2022
8m0 commentedon Dec 13, 2022
Majestic-Mustang commentedon Dec 13, 2022
I have the same problem. cc: @PureWeen
mohachouch commentedon Dec 13, 2022
@jsuarezruiz @PureWeen @jamesmontemagno @maddymontaquila @davidortinau same problem on Xamarin Forms
We spent 3 weeks, 2 developers trying to figure out why the pages are not cleaning up. Unable to find. We just see in the Xamarin Profiler that the Resource Dictionary memory increases
mohachouch commentedon Jan 4, 2023
PureWeen commentedon Jan 8, 2023
related #12344
nk54 commentedon Jan 12, 2023
From @8m0
This one made me laugh more than expected after I saw the bot moving this thread to the backlog milestone
I din't expect Xamarin to be memory leak proof because it wasn't a tech from Microsoft at first. But I expected MAUI to be better. Far better than its current state about momery leaks...
rkebelj commentedon Jan 16, 2023
This is so sad.
59 remaining items
hosamyousof commentedon Feb 23, 2023
Hi @jamesmontemagno @PureWeen @jsuarezruiz @samhouts
Please, when we are going to have this update #13327 in .net 6 and .net 7??
jsuarezruiz commentedon Feb 23, 2023
Will take a look to move the changes to Xamarin.Forms.
mohachouch commentedon Feb 23, 2023
Thanks @jsuarezruiz
AlasaliCS commentedon Feb 26, 2023
when will you update it for .net6 ? this is a serious and urgent problem need to be solved.
thank you
[core] WeakEventManager.RemoveEventHandler() should clear subscriptions
[core] WeakEventManager.RemoveEventHandler() should clear subscriptions
[release/7.0.2xx] [net7.0] [core] WeakEventManager.RemoveEventHandler…
jsuarezruiz commentedon Mar 22, 2023
Changes already available in latest Xamarin.Forms SR.
ToolmakerSteve commentedon Apr 15, 2023
"the amount of memory leaks, which makes the app completely unusable."
With and without
vm.Bytes = null;, did you run until the app became sluggish? Without debugger attached? That is the only way to prove that it is truly a GC problem, not just "deferred GC".Large memory allocations have always been problematic in .Net. Try a list of arrays, where each list element (one array) is small enough to fit on object heap. Behave similarly? If you
nulleach list element, then do List.Clear(), does that help?