maui: Memory leaks EVERYWHERE
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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 33
- Comments: 44 (12 by maintainers)
Commits related to this issue
- [core] `WeakEventManager+Subscription` needs `IEquatable` Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 Using the Visual Stu... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [core] `WeakEventManager+Subscription` needs `IEquatable` Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 Using the Visual Stu... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [core] `WeakEventManager+Subscription` needs `IEquatable` (#13232) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 Using th... — committed to dotnet/maui by jonathanpeppers a year ago
- [controls] fix memory leak in Style and AttachedCollection Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/memory-leak Fixes: https://github.com/dotnet/maui/issues/12039 This is still W... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [controls] fix memory leak in Style and AttachedCollection Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/memory-leak ... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [controls] one less WeakReference in TypedBinding Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 When reviewing the memory sn... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [controls] one less WeakReference in TypedBinding Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 When reviewing the memory sn... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [controls] fix memory leak in Style and AttachedCollection (#13260) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/me... — committed to dotnet/maui by jonathanpeppers a year ago
- [controls] one less WeakReference in TypedBinding (#13304) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 When reviewing t... — committed to dotnet/maui by jonathanpeppers a year ago
- [core] fix memory leaks in bindings Fixes: https://github.com/dotnet/maui/issues/12039 Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/symbiogenesis/Mau... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [core] fix memory leaks in bindings Fixes: https://github.com/dotnet/maui/issues/12039 Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/symbiogenesis/Mau... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [core] WeakEventManager.RemoveEventHandler() should clear subscriptions Context: https://github.com/dotnet/maui/issues/12039 Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [core] WeakEventManager.RemoveEventHandler() should clear subscriptions (#13333) * [core] WeakEventManager.RemoveEventHandler() should clear subscriptions Context: https://github.com/dotnet/maui/i... — committed to dotnet/maui by jonathanpeppers a year ago
- [core] fix memory leaks in bindings (#13327) Fixes: https://github.com/dotnet/maui/issues/12039 Fixes: https://github.com/dotnet/maui/issues/10560 Context: https://github.com/Vroomer/MAUI-master-de... — committed to dotnet/maui by jonathanpeppers a year ago
- Merge net8.0 into release/8.0.1xx-preview1 (#13441) * Added samples * Fix the bug * Added device tests * Microsoft.Maui.ApplicationModel.Permissions: add Bluetooth permissions for Android (#... — committed to dotnet/maui by Redth a year ago
- [core] `WeakEventManager+Subscription` needs `IEquatable` (#13232) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 Using the V... — committed to tj-devel709/maui by jonathanpeppers a year ago
- [controls] fix memory leak in Style and AttachedCollection (#13260) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/me... — committed to tj-devel709/maui by jonathanpeppers a year ago
- [controls] one less WeakReference in TypedBinding (#13304) Context: https://github.com/Vroomer/MAUI-master-detail-memory-leak Context: https://github.com/dotnet/maui/issues/12039 When reviewing t... — committed to tj-devel709/maui by jonathanpeppers a year ago
- [core] WeakEventManager.RemoveEventHandler() should clear subscriptions (#13333) * [core] WeakEventManager.RemoveEventHandler() should clear subscriptions Context: https://github.com/dotnet/maui/i... — committed to tj-devel709/maui by jonathanpeppers a year ago
- [core] fix memory leaks in bindings (#13327) Fixes: https://github.com/dotnet/maui/issues/12039 Fixes: https://github.com/dotnet/maui/issues/10560 Context: https://github.com/Vroomer/MAUI-master-de... — committed to tj-devel709/maui by jonathanpeppers a year ago
We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
I think this is on the right track: https://github.com/dotnet/maui/pull/13260
Between the memory leaks and the inefficiency of the CollectionView and other controls, and the Shell having minor bugs, how can we trust MAUI?
This is so sad.
@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
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.
Hi @jamesmontemagno @PureWeen @jsuarezruiz @samhouts
Please, when we are going to have this update #13327 in .net 6 and .net 7??
It seems like I found something.
Snapshot 2:
Snapshot 10:
I will keep investigating.
Same problem. I have only one question. How to work with MAUI with such bugs?
Will take a look to move the changes to Xamarin.Forms.
I have the same problem. cc: @PureWeen
As I said above. We have the same problem on Xamarin Forms on a large application. We have 15000 daily users for enterprise application. Xamarin Forms support is until May 2024. Please fix and don’t forget Xamarin Forms users. @jonathanpeppers @jamesmontemagno @PureWeen @jsuarezruiz
@symbiogenesis I have big app ready for production and I can’t release it because it goes from 200 MB to 2000 MB in matter of a few minutes of using it. It’s by far the biggest problem I encountered as there is no workaround. It’s not data objects being stuck, it’s all just UI bits being held by strong references from brushes, gesture handlers and god knows what else. The problem is really serious. No events or things like that are causing my memory leaks. I either make sure to detach events or make them as a weak reference.
There isn’t an excuse for memory leaks on the platform, but the patterns that are likely to cause memory leaks are likely not great patterns for normal workloads. You shouldn’t be abusing query parameters, for instance. And the leaks aren’t any worse than Xamarin.Forms was. In my case, XF was worse. Until the platform bugs are fixed, the best you can do is focus on your own code.
Events are one of the most likely causes of leaks, and those can be solved if you implement WeakEventManager. That one is pretty easy to do.
I had seen worse leaks from the handlers in XF, particularly inside of CollectionViews. You could scroll and just watch the memory usage rise and rise endlessly. I think a lot of those issues were solved with MAUI. I don’t see this at all with my app.
Premature optimization is a bad idea. Unless your app has a known memory issue, there is no reason to assume that it does. For me, converting from XF to MAUI caused a measurable and dramatic decrease in memory usage.
Changes already available in latest Xamarin.Forms SR.
when will you update it for .net6 ? this is a serious and urgent problem need to be solved. thank you
Hi @jonathanpeppers, Thanks for the fix of this bug. We have the same bug on Xamarin Forms. Can you do a PR also for Xamarin Forms. Thanks
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…
I tried this app: https://github.com/Vroomer/MAUI-master-detail-memory-leak.git
Using latest dotnet/maui/main. I changed one thing, just to try to make the GC button slightly better:
Then I took an initial snapshot, and did
CollectionViewclick,Pop page,Force GCgetting a memory snapshot each time:(notice the flat memory graph)
Initially, my thought that I would see new instances of these appearing (and that was the leak):
Bytes = new byte[1024 * 400000]DetailPageobjectsEntryobjectsHowever, I don’t see any of these objects being leaked.
To cross-verify this, I ran
dotnet gcdump collect -p 29096:https://devblogs.microsoft.com/dotnet/collecting-and-analyzing-memory-dumps/
Opening the dump, I see the expected bytes (and it does not grow as I navigate more):
I am not really seeing a problem currently with this sample & dotnet/maui/main.
Does anyone have a suggestion of something else I can try?
I will try the latest stable MAUI bits.
Hello, thanks for the answer. Could you do something regarding to the problem? It’s very important problem which always got pushed to Backlog milestone.
Just wrote a comment about the problem under @jamesmontemagno latest video, the comment was deleted within 1 min.
You were right. I replaced CollectionView with SfListView from Syncfusion and the problem is gone, no more memory leak when refreshing the VM and scrolling. Already got a Syncfusion community license, so now I can keep working
I am very frustrated with MAUI, I proposed it at the company I work for as a good framework for cross platform app development. But we find memory errors and disastrous performance in very important controls that prevent us from taking an application to production.
Looks like this custom ListView control advertises that it avoids memory leaks
https://github.com/Keflon/FunctionZero.Maui.Controls
This one may also be useful, but only for Android and iOS:
https://github.com/roubachof/Sharpnado.CollectionView
this one also seems promising:
https://github.com/Redth/Maui.VirtualListView
I loaded up thousands of records into a CollectionView today, and noticed that the scrolling on WinUI was actually smooth. Android and iOS were bad.
Looks like the recent PRs https://github.com/dotnet/maui/pull/12627 and https://github.com/dotnet/maui/pull/12660 will be helping.
Some tricks to try now include:
These tricks are all used by @angelru already, but not everyone may be aware.
In addition to those tricks and the aforementioned WeakReferences for events, other things to try: