maui: Memory Leak issue in ListView

Description

I have a CheckBox in the ListView DataTemplate and am switching the ListView’s ItemsSource at runtime. Due to this, the app size gradually increases and eventually leads to a crash of the application.

https://github.com/dotnet/maui/assets/93904422/9a793432-fa67-488a-a07d-5ccb28fa655b

Steps to Reproduce

  1. Run the given sample.
  2. Continuously switch the ItemsSource.

Link to public reproduction project repository

https://github.com/Tamilarasan-Paranthaman/Maui-Itemssource-Switching-Issue

Version with bug

7.0.86

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 25 (12 by maintainers)

Commits related to this issue

Most upvoted comments

So, it looks like even if you have an empty <ViewCell/> your sample app leaks:

image

Notice the overall heap size at the end got up to 3,964 kb.

I looked here:

image

The problem is every Cell subscribes:

https://github.com/dotnet/maui/blob/9fcccd57b5a3d664e4788b3c7fc3edc10010beaf/src/Controls/src/Core/Cells/Cell.cs#L197-L201

And so the one ListView keeps all these cells alive.

Now I do think everything would go away if you navigated away from this page. Or a workaround would be to create two ListView and hide/show them instead.