Skip to content

Memory leaks EVERYWHERE #12039

Closed
Closed

Description

@Vroomer

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

Activity

  1. added
    t/bug
    on Dec 12, 2022
  2. added
    legacy-area-perf
    on Dec 13, 2022
  3. 8m0 commented on Dec 13, 2022

    @8m0

    If you keep navigating back and forth from master to its details and watch memory usage, you will see memory raising

    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.

  4. angelru commented on Dec 13, 2022

    @angelru
  5. 8m0 commented on Dec 13, 2022

    @8m0
  6. Majestic-Mustang commented on Dec 13, 2022

    @Majestic-Mustang

    I have the same problem. cc: @PureWeen

  7. mohachouch commented on Dec 13, 2022

    @mohachouch
    Contributor

    @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

  8. added this to the Backlog milestone on Dec 13, 2022
  9. added
    p/1
    on Dec 17, 2022
  10. mohachouch commented on Jan 4, 2023

    @mohachouch
  11. PureWeen commented on Jan 8, 2023

    @PureWeen
    Member

    related #12344

  12. nk54 commented on Jan 12, 2023

    @nk54

    From @8m0

    I think this problem will be moved to "Backlog milestone" as always.

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

  13. rkebelj commented on Jan 16, 2023

    @rkebelj

    This is so sad.

  14. 59 remaining items

  15. hosamyousof commented on Feb 23, 2023

    @hosamyousof

    Hi @jamesmontemagno @PureWeen @jsuarezruiz @samhouts

    Please, when we are going to have this update #13327 in .net 6 and .net 7??

  16. jsuarezruiz commented on Feb 23, 2023

    @jsuarezruiz
    Contributor

    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

    Will take a look to move the changes to Xamarin.Forms.

  17. mohachouch commented on Feb 23, 2023

    @mohachouch
    Contributor

    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

    Will take a look to move the changes to Xamarin.Forms.

    Thanks @jsuarezruiz

  18. AlasaliCS commented on Feb 26, 2023

    @AlasaliCS

    when will you update it for .net6 ? this is a serious and urgent problem need to be solved.
    thank you

  19. jsuarezruiz commented on Mar 22, 2023

    @jsuarezruiz
    Contributor

    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

    Will take a look to move the changes to Xamarin.Forms.

    Changes already available in latest Xamarin.Forms SR.

  20. ToolmakerSteve commented on Apr 15, 2023

    @ToolmakerSteve

    "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 null each list element, then do List.Clear(), does that help?

  21. ghost locked as resolved and limited conversation to collaborators on May 16, 2023
  22. added
    perf/general
    on May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

delighterfixed-in-8.0.0-preview.1.7762legacy-area-perfp/0partner/cat 😻perf/generalplatform/windowst/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions