dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

[Windows] UI Virtualization broken in Listviews #24343

Open Larhei opened 3 weeks ago

Larhei commented 3 weeks ago

Description

I´m not an expert on this topic but from my experience in other xaml stacks this is looking fishy to me.

When using a ListView or Collectionview on Windows bound to a collection of 1000 items i observe the folowing. When scrolling this itemscollections fast i end up with 3000+ instances of a contentview used in the Datatemplate. In addition to 300+ instances of Microsoft.UI.Xaml.Controls.ListViewItem on the native side of things. Beside the fact that the Cachelength of the ItemsStackpanel is 4 and my ViewportHeight is 500 and each Item has a Height of 100. There should be round about 8 Instances of ListViewItem from my understanding and maybe also less instances of my Datatemplate on the Maui Side. Over 500 MB for an App only showing two Lists with an empty View just looks wrong to me.

Steps to Reproduce

  1. Check out Sample on WIndows.
  2. Run Sample on Windows.
  3. Scroll the Listview a view times up and down. Watch the output while scrolling.
  4. Scroll the Collectionview up and down. Watch the output while scrolling.
  5. Take a few Snapshots in order to kick GC
  6. Compare Baseline to Last Snapshot

Link to public reproduction project repository

https://github.com/Larhei/Maui-Issues/tree/main/ListVirtualisation

Version with bug

8.0.80 SR8

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.19041

Did you find any workaround?

Nope

Relevant log output

None
github-actions[bot] commented 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!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Larhei commented 3 weeks ago

Tested also without Hotreload and in Release. Applying Border like in first similar issue did not solve the issue :-(

MitchBomcanhao commented 3 weeks ago

are you putting your listview inside a stack layout? somehow I recall reading somewhere that you shouldn't do that because it'd not contain the list controls and would force the listview to initialise everything, resulting in what you're complaining about. use a grid instead of a stack layout.

MitchBomcanhao commented 3 weeks ago

asked copilot:

Yes, placing a ListView inside a StackLayout in Xamarin.Forms can indeed break virtualization.
This is because the StackLayout measures its children with an infinite height, causing the ListView 
to load all its items at once instead of virtualizing them. 
This can lead to performance issues, especially with large data sets.

To maintain virtualization, you can use a Grid or AbsoluteLayout instead of a StackLayout
Larhei commented 3 weeks ago

@MitchBomcanhao Well i specify a Height to my ListViews and Collectionview. So the native side should know my the viewport height... But I will change my layout and see what i come up with.

Larhei commented 3 weeks ago

Changing to Grid did not do the trick image image image

Memory still over 300mb

MitchBomcanhao commented 3 weeks ago

yes, when taking snapshots I also see the number of objects increasing when using a grid. oddly enough, when adding Border around my control (which was just a label), it made the presentation noticeably slower, with viewcells taking much longer to draw.

kevinxufei commented 3 weeks ago

This issue has been verified using Visual Studio 17.12 Preview 1 (8.0.80 & 8.0.72). Can repro on windows platform with sample project.