question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leaks EVERYWHERE

See original GitHub issue

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.

742nlk

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. image

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.

image

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

  1. Load the repository and run the app.
  2. Repeatedly open details from CollectionView by selecting them and then pop the DetailPage using the button while observing memory usage in ResMon/memory profiler.
  3. 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:closed
  • Created 9 months ago
  • Reactions:33
  • Comments:44 (12 by maintainers)

github_iconTop GitHub Comments

34reactions
msftbot[bot]commented, Dec 13, 2022

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.

13reactions
jonathanpepperscommented, Feb 10, 2023

I think this is on the right track: https://github.com/dotnet/maui/pull/13260

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Memory Leaks in Java
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector...
Read more >
Preventing and detecting memory leaks in Android apps
Memory leaks can cause your Android app to crash, causing frustration and lower usage. Learn how to fix them in this guide.
Read more >
Fixing memory leaks in web applications | Read the Tea Leaves
One of these problems is memory leaks. A poorly-coded SPA can easily eat up megabytes or even gigabytes of memory, continuing to gobble...
Read more >
Avoiding memory leaks when using Data Binding and View ...
Memory leaks everywhere​​ When using Data Binding we used to add the binding as a lateinit var to our fragment like this: This...
Read more >
Memory leaks in pages : r/dotnetMAUI
MAUI has massive memory leaks everywhere, it's acknowledged by the devs, and won't be in the stable version before the next release in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found