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
- Run the given sample.
- 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
- [windows] fix memory leak in ListView Context: https://github.com/Tamilarasan-Paranthaman/Maui-Itemssource-Switching-Issue Fixes: https://github.com/dotnet/maui/issues/16450 In a customer sample, sw... — committed to jonathanpeppers/maui by jonathanpeppers a year ago
- [windows] fix memory leak in ListView (#16762) * [windows] fix memory leak in ListView Context: https://github.com/Tamilarasan-Paranthaman/Maui-Itemssource-Switching-Issue Fixes: https://github.c... — committed to dotnet/maui by jonathanpeppers 10 months ago
- [windows] fix memory leak in ListView (#16762) * [windows] fix memory leak in ListView Context: https://github.com/Tamilarasan-Paranthaman/Maui-Itemssource-Switching-Issue Fixes: https://github.c... — committed to dotnet/maui by jonathanpeppers 10 months ago
So, it looks like even if you have an empty
<ViewCell/>your sample app leaks:Notice the overall heap size at the end got up to 3,964 kb.
I looked here:
The problem is every
Cellsubscribes:https://github.com/dotnet/maui/blob/9fcccd57b5a3d664e4788b3c7fc3edc10010beaf/src/Controls/src/Core/Cells/Cell.cs#L197-L201
And so the one
ListViewkeeps 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
ListViewand hide/show them instead.