We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
Lately I was working on an application that had to display a large amount of objects on screen and allow filtering. I have learned that scrolling large collections was not so simple in WPF, and I definitely did not see the problems coming.
Here is an example of a simple WPF application which randomly creates 50000 Classes of type DATA in a list and displays them in an Item control:
Here is the Xaml of the application:
And the Code Behind:
Very Simple. If the integer is positive, the border and fill are green, if its negative its red.
Here is a screen shot of the Task Manager once this application is up and running:
A Whopping 500MB of RAM!!! I thought to myself that it is because I am holding 50000 Class objects and I was wrong. Here is the task manager right after the List Creation:
Barely 7MB of Ram. This means that drawing the Classes takes 500MB of ram. Be careful when you are working with Large Collection.
If you would run the previous example you will notice that the scrolling performance is terrible. It is not smooth and scrolls in Blocks, there is a small overhead in memory consumption, The reason it is not smooth is that every time you scroll new objects are created to show the data.
If the Templates are complicated you will notice a spike in the Memory Consumption. Here the charge is small but I have seen applications that simply by scrolling you add 50% to the Memory consumption of the application, not very nice…
You can solve this easily by ordering WPF to recycle objects, just like in real life :).
all you have to do is add two attributes to the items control:
No more Wasting of good Templates that have already been created, and a big improvement to performance and Memory Consumption.
So be careful when binding to large collections.
And don’t forget to recycle!
Tags :ItemsComtrolLarge CollectionsMemory ComsumptionRecyclingScrollingVirtualization
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd