Open jari-schuurman opened 3 weeks ago
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds
Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds
@PureWeen I have installed the 8.0.60-ci.net8.24309.1 nightly build:
As you can see in the screenshot, it still keeps a lot of references to the GroupedItemTemplateCollection
and it is still a factor of 7 compared to my collection (GroupedCollection
). I'd say the memory leak is still present in this nightly build.
This is the results after applying my workaround:
Added repro repo
Verified this issue with Visual Studio 17.11.0 Preview 2.0 (8.0.60 & 8.0.40& 8.0.3). Can repro on Windows platform with sample project.
Any update on this? This basically makes grouped collections unusable on Windows platform :/
@PureWeen any estimation when this can be resolved?
Description
CollectionView & ObservableCollections
If you use
CollectionView
withIsGrouped=true
where yourItemsSource
is of typeObservableCollection
; congratulations you just introduced a memory leak in your application! Don't worry: the fix is easy! If it ever gets picked up by the MAUI team ;)If you use grouping, the internal source for
CollectionViewSource
will be of type https://github.com/dotnet/maui/blob/b182ffef2c85a5681686732a81b0f572a86591cc/src/Controls/src/Core/Platform/Windows/CollectionView/GroupedItemTemplateCollection.cs#L9 see: https://github.com/dotnet/maui/blob/b182ffef2c85a5681686732a81b0f572a86591cc/src/Controls/src/Core/Handlers/Items/GroupableItemsViewHandler.Windows.cs#L19Inside the
GroupedItemTemplateCollection
there is a check to see if the itemssource implementsINotifyCollectionChanged
:Maybe you already feel it coming or not... But yes indeed this event is never unsubscribed + the
GroupsChanged
event is not a static method. Which according to their docs does introduce a leak: https://github.com/dotnet/maui/wiki/Memory-Leaks#when-we-cant-use-weakeventmanagerI have spent many, way too many, hours testing, analyzing MAUI source code, verifying and more testing to confirm this is the root cause of the issue.
Test setup
I have a basic mainpage with a viewmodel. In the viewmodel I open a popup with a
CollectionView
as the content of the popup. I have my own sub class of aCollectionView
with adestructor
added so I can verify if it is cleaned up or not (it is not).Memory Usage/Snapshot & analysis
What I noticed is that the
Count
of theGroupedItemTemplateCollection
is always 7x theCount
of theCollectionView
. I guess it has to do with https://github.com/dotnet/maui/blob/b182ffef2c85a5681686732a81b0f572a86591cc/src/Controls/src/Core/Handlers/Items/GroupableItemsViewHandler.cs#L20 where it will trigger theUpdateItemsSource
multiple times. In here theCleanUpCollectionViewSource
is executed: https://github.com/dotnet/maui/blob/b182ffef2c85a5681686732a81b0f572a86591cc/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs#L156My idea is that the
GroupableItemsViewHandler
should override theCleanUpCollectionViewSource
and check if thesource
isGroupedItemTemplateCollection
. Since this handler is also responsible for creating this type of collection. Then do aCleanUp
for example:However, since this is not happening at the moment. Every time the
UpdateItemsSource
is triggered, a new memory leak is introduced.The solution
Either make the
GroupsChanged
static or follow https://github.com/dotnet/maui/wiki/Memory-Leaks#when-we-cant-use-weakeventmanagerI hope people can follow my explanation, I know I am not the best with explaining such things. Ill publish a repro repo soon, together with the workaround... well "workaround"
Steps to Reproduce
CollectionView
withIsGrouped=true
ItemsSource
binding is set to anObservableCollection
Link to public reproduction project repository
https://github.com/jari-schuurman/GroupFooterTemplateIssue
Version with bug
8.0.3 GA
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
windows10.0.19401.0
Did you find any workaround?
In order to fix it temporarily, you will have to do some magic:
CollectionView
:GroupedCollection
. You do not need to add any custom properties.Handler
for this control that inherit fromCollectionViewHandler
, e.g.:GroupedCollectionHandler
Handler
to your maui program:handlers.AddHandler(typeof(GroupedCollection), typeof(GroupedCollectionHandler));
internal
so we cannot easily change them:public class CleanObservableCollection : ObservableCollection, ICleanObservableCollection
{
///
/// Initializes a new instance of CleanObservableCollection that is empty and has default initial capacity.
///
public CleanObservableCollection()
{
}
}
I didn't take the time to see if we can make this check more performant, but for now it works :)
CleanObservableCollection
for yourItemsSource
binding and use the custom control created in step1
instead of theCollectionView
addHandler
step with the #if WINDOWS #endif tagsRelevant log output
No response