Memory leaks EVERYWHERE
See original GitHub issueDescription
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
Issue Analytics
- State:
- Created 9 months ago
- Reactions:33
- Comments:44 (12 by maintainers)
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