-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CollectionView] [Windows] memory leaks when ItemsSource has changed #13393
Comments
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. |
Could be related with #13327 |
Yes, I think some of these recent fixes might solve this one:
But I will test this case with main next week. |
Those leaks are a major issue and an absolute show stopper for our app causing the dotnet-maui version not going live. It would be so great to have those fixes in the next service release of the dotnet 7 version. |
Fixes: dotnet#13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. WIP to determine if we can write a test or if this happens on any other platforms. Android seems OK.
@ivan-todorov-progress I did indeed find an issue with your sample app above: #13530 Do you know if this only happens on Windows? Android actually seems like it is OK without me changing anything: |
Fixes: dotnet#13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android.
@jonathanpeppers I can confirm this is not happening on Android. My speculation is that on Android the CollectionView clears the BindingContext of its items when they are recycled, so the Bindings get disconnected and we have no memory leak. On Windows it is easily reproducible, however. I have not had the chance to test it with the latest version of Visual Studio and .NET MAUI to see whether the leak is fixed. I'll keep you posted with my findings, once I upgrade. |
@jonathanpeppers I have updated Visual Studio to the latest 17.5 version and re-tested the sample app on all platforms. On Android and iOS there is no memory leak. The memory leak on Windows is still there, though. I believe, we need your fix about ClearLogicalChildren as well: jonathanpeppers@c7837db. I have updated the bug title and description to make it clear it is a Windows-only issue, in case someone else is tracking this. P.S.: Great job fixing all these memory-related problems in .NET MAUI! |
…13530) Fixes: #13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android.
Fixes: #13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android.
Fixes: #13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android.
…View.ItemsSource` changes (#14075) * [windows] fix memory leak when `CollectionView.ItemsSource` changes Fixes: #13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android. * Fix for test on iOS Apparently iOS is only creating 1: Assert.Equal() Failure\nExpected: 3\nActual: 1 * Auto-format source code --------- Co-authored-by: Jonathan Peppers <[email protected]> Co-authored-by: GitHub Actions Autoformatter <[email protected]>
Hello @jonathanpeppers, I know this issue is closed some weeks ago. Nevertheless, can you please shortly comment on my first question on this PR? Does you change fixes ObservableCollection behavior as well? |
Hi @rmarinho, I can see this fix got merged to release/7.0.2xx. Can you please shortly explain or give reference to the release model? Is there any description that will explain which Visual Studio version will bring this fix to my local environment so I can try to verify and experiment? Thank you very much in advance. |
Description
It seems that the
CollectionView
is leaking memory every time itsItemsSource
property has changed.Due to a lack of a proper memory profiler on all platforms, I am using a poor man's memory tracker implementation that stores a collection of
WeakReference
s and observes theirIsAlive
status:I am using this class to track visual components in XAML like this:
I have prepared a small sample project that demonstrates the memory leak.
Note: It is possible that the following bug is related: Memory leaks EVERYWHERE. Since I am describing a somewhat different scenario, I have decided to log a separate bug report for it.
Steps to Reproduce
CollectionView
in the visual tree.CollectionView
is recreated.Note: Clicking the "Refresh Memory Info" button calls the garbage collector forcefully before updating the memory statistics:
Link to public reproduction project repository
https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
N/A
Did you find any workaround?
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: