Skip to content

CollectionView does not appear to reuse Templated controls #23285

Open

Description

Description

I am really struggling with virtualization in CollectionView and this is making it really hard for me to optimise my app and get it to run at any decent speed in CollectionView. The control I am using in the DataTemplate is quite expensive to generate and never seems to be garbage collected. Ideally I want to recycle it rather than re-generate it or GC.

My expectation is that in collection view when you change the underlying data due to virtualization the controls in the data template would be reused and just the DataContext would get re-mapped. On IOS this is what appears to happen but on Android it just appears to be re-generating the control and you end up with many thousands of control instances on a grid of 100 items.

I have put together a simple example that prints to the output whenever the control is instantiated and destroyed. I would expect on the first fill you would see lots of controls being created but on the second press of the fill no controls being created. However on Android it creates controls on every fill and the unused ones have to be Garbage collected.

[https://github.com/owaits/MauiLayoutAndSizingStrategy]

My actual code is using a template selector but this issue can be seen on a simple data template.

How am I supposed to avoid the expense of re-building the control every time the collection data changes.

Steps to Reproduce

Run the app https://github.com/owaits/MauiLayoutAndSizingStrategy on Android
Click on "Fill Items"
Note "Instances Plus x" is printed to output
Click "Fill Items" I would expect no instances to be created

Link to public reproduction project repository

https://github.com/owaits/MauiLayoutAndSizingStrategy

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

Any suggestions on a way to work around this would be appreciated.

Relevant log output

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-controls-collectionviewCollectionView, CarouselView, IndicatorViewplatform/android 🤖s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions