Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Top 11 WPF Performance Tips

Windows Presentation Foundation provides a very confortable way to develop rich user experiences. A drop shadow for example can added by inserting two simple lines of XML. But this simplicity can also mislead us to overuse them. This leads to performance issues. The following tipps may help you to avoid or fix them.

  1. Dispatch expensive calls either within the UI thread with a lower DispatcherPriority by calling Dispatcher.BeginInvoke() or to a background thread by using a BackgroundWorker to keep the UI responsive.
  2. Fix binding errors because they consume a lot of time, trying to resolve the path error, including searching for attached properties. You can find them by looking for System.Windows.Data Error in the Visual Studio output log.
  3. Reduce the number of visuals by removing unneeded elements, combining layout panels and simplifying templates. This keeps the memory footprint small and improves the rendering performance.
  4. Prevent Software Rendering. The use of transparent windows by setting AllowsTransparency to true or using old BitmapEffects can cause WPF to render the UI in software on Windows XP, which is much slower.
  5. Load resources when needed. Even thow it's the most comfortable way to merge all resources on application level it can also cost performance by loading all resources at startup. A better approach is to load only often used resources and load the other on view level.
  6. Virtualize lists and views by using a VirtualizingStackPanel as ItemsPanel for lists. This only creates the visible elements at load time. All other elements are lazy created when they get visible. Be aware that grouping or CanContextScrol="True" prevents virtualization!
  7. Enable Container Recycling. Virtualization brings a lot of performance improvements, but the containers will be disposed and re created, this is the default. But you can gain more performance by recycle containers by setting VirtualizingStackPanel.VirtualizationMode="Recycling"
  8. Freeze Freezables by calling Freeze() in code or PresentationOptions:Freeze="true" in XAML. This reduces memory consumption and improves performance, because the system don't need to monitor for changes.
  9. Disable Assembly localization if you don't need it. By using the [NeutralResourcesLanguageAttribute]. This prevents an expensive lookup for satelite assemblies
  10. Lower the framerate of animations by setting Storyboard.DesiredFrameRate to lower the CPU load. The default is 60 frames/second
  11. Use StreamGeometries instead of PathGeometries if possible to draw complex 2D geometries, because they are much more efficient and consume less memory.

Other articles about WPF performance

WPF Performance Tools

There are two interesting tools to trace your WPF application and narrow down performance leaks:





Last modified: 2011-03-02 17:32:34
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Rafa
Commented on 11.May 2010
haha, almost there.. 49 to go
oyeeee
Commented on 11.May 2010
Good Tips, dont write anything and always fast:))
Brad.A
Commented on 13.May 2010
Come on I'm waiting for the other 49 dude!!!!!!!
Alexej
Commented on 14.May 2010
I look also for other 49 :)
Roman
Commented on 16.May 2010
After you couldn't find 50 performance tips, you switched to a Top 10.
And now you're showing us a Top 9 ??
It would be better if you completed articles before posting them.

Besides that, useful information. Thanks!
Christian Moser
Commented on 17.May 2010
Hi Roman,
sorry for that. I added the missing 10th :-)
Ben Dover
Commented on 18.May 2010
Great article, keep up the good work, your blog is a very informative and useful.
Alejandro Nagy
Commented on 18.May 2010
Thanks for the tips mate, they're useful for me. I'm just starting a migration from Winforms to WPF. On a side note, is sad to see what jealousy does to people...why don't they try to make a great resource page like this one...then we'll see if they find 50 tips to share.
Radenko Zec
Commented on 21.May 2010
Some of nice tips.Thanks
Marv Luse
Commented on 21.May 2010
Moore's Law implies that we should be able to ignore these performance issues. Unfortunately, there's Marv's Law: the resource requirements of Windows doubles every 15 or 20 minutes. Thaaaat's wwwhhhyyy duuuh-buuu-yoooo peeeee eeeeef iiiiiiiisssss sssssoooooooo ssssssslloooooowwwww.
RaeNO
Commented on 21.May 2010
What about WPFPerf Suite for .Net 4.0?
Mysticool
Commented on 23.August 2010
How much cash .. mind sharing..lol..J/K.. keep up the hunting for us. you should call this WPF HUB
Denis
Commented on 7.September 2010
It works in Silverlight
12. x:Name only the elements you need to use from code-behind or want to "Control2Control" bind.
Elad Katz
Commented on 18.November 2010
Great post!

Just one comment though - Freezable object are for thread safety and not as u said..
http://blogs.microsoft.co.il/blogs/eladkatz/archive/2010/11/18/freezable-objects-it-s-about-thread-safety.aspx
Jeya
Commented on 30.November 2010
Good Tutorial for beginners. Thanks a lot Christian...
Nilesh Makwana
Commented on 9.December 2010
Good Stuff, man!
Tinoy Malayil
Commented on 8.January 2011
Which will be better ?...to write code in c# or xaml to get good peformance
jai
Commented on 16.February 2011
hi
Darren Bruce
Commented on 2.March 2011
Tip 5: What is the word Thow? Pretty sure this doesnt even exist in the americanized incorrect version of the english language
Musab Ahmed Ali
Commented on 14.March 2011
small correction in 6:
ScrollViewer.CanContentScroll="False" instead of CanContextScrol="True"
len
Commented on 23.March 2011
Tip 4: "setting AllowsTransparency to true can cause WPF to render the UI in software on Windows XP, which is much slower." -- Is it still true for now? Is there any fix for it?
Bean
Commented on 21.July 2011
I don't know what these guys are talking about !
Bean
Commented on 21.July 2011
I don't know what these guys are talking about !
adee.
Commented on 27.August 2011
good
Lakmal
Commented on 3.September 2011
Great Stuff man..

Name
E-Mail (optional)
Comment