Training for D-Day

ブログの内容は個人の見解であり、所属する企業を代表するものではありません。

WPFパフォーマンス関連の記事まとめ

WPFは非常に高機能ですが、扱い方を間違えると大火傷を負います。 簡単にですが、頭の片隅に残しておくべきWPFのパフォーマンス関連の記事をまとめました。

はじめに

MSDN

まず気をつけるべきことが記載されています。

https://msdn.microsoft.com/ja-jp/library/aa970683%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/ff647792.aspx#scalenetchapt04%20_topic14

気をつけるべき10のことみたいなやつ

WPFアプリのパフォーマンスを良くする10の方法

http://www.codeguru.com/csharp/.net/net_wpf/article.php/c18025/10-Ways-to-Improve-the-Performance-of-Your-WPF-Application.htm

  1. BitmapImageのスケールを小さくしろ
  2. FlowDocument内にTextBlock使わないでRun使え
  3. DynamicResourceではなくStaticResource使え
  4. でかいオブジェクトを描画したい場合はUI Virtualization(仮想化) 使え
  5. リッチじゃないUIだったらScrollは Deferredを指定せよ
  6. 起動時間を速くさせたいなら、PresentationFontCache Serviceを起動させておけ
  7. 不要になったアニメーションはUnloadedイベントでRemoveせよ
  8. Containerのプロパティは可能であればRecycle使え
  9. ハードウェアレンダリングかソフトウェアレンダリングかの識別はRenderCapability.Tier使えばできるよ
  10. 調査用ツールを使え

WPFのパフォーマンスを改善する12の方法

http://pelebyte.net/blog/2011/07/11/twelve-ways-to-improve-wpf-performance/

  1. Binding Errorを回避せよ( PrismのBindingBaseBindableBase/NotificationObjectであればデバッグ時にVisual Studioデバッグ出力ウィンドウに警告が出力されます)
  2. 可能であれば高さと幅は固定にしろ(XAML上にハードコードしろ)
  3. CollectionViewのGrouping機能は使うな
  4. コレクション(ObservableCollection) への変更通知を最適化せよ (ObservableCollectionは要素をAddするたびに変更通知走っちゃう )
  5. DynamicResourceは避けよ
  6. ResourceDictionaryは避けよ ResourceDictionaryを生成するのはすごいコストだぜ
  7. Visual Treeを Simpleに保て
  8. System.Windows.Interactivity.Behavior.OnDetachingは呼ばれないことがあるので注意してください
  9. いくつかの理由により、DependencyPropertyDescriptorは使うな
  10. ViewModelのイベントに注意せよ(WeakEventManager使ってリークしないようにしようね)
  11. でかいデータを取得するならDispatcher.BeginInvokeで(よくわからないが非同期処理しろってことかな)
  12. メモリリークに常に注意を払え

WPFパフォーマンス 10Tips

http://www.wpftutorial.net/10PerformanceTips.html

  1. BackgroundWorkerなりBeginInvokeなりで処理しろ
  2. Binding Errorを回避せよ
  3. Visual の数を減らせ(Visual TreeをSimpleに保て )
  4. ハードウェアレンダリングを防げ
  5. Resourceは必要になったときに読め
  6. 仮想化せよ(VirtualizingStackPanel利用のところは)
  7. Containerのプロパティは可能であればRecycle使え
  8. FreezeできるところはFreezeしろ(PresentationOptions:Freeze="true")
  9. アセンブリのロケーションをDisableに
  10. アニメーションのフレームレートを下げろ
  11. PathGeometriesじゃなくてStreamGeometriesを使え

調査用ツール

WPF Performance Suite レンダリング関連に強い。

https://msdn.microsoft.com/ja-jp/library/aa969767%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

http://blogs.msdn.com/b/jgoldb/archive/2010/05/25/wpfperf-performance-profiling-tools-for-wpf-4-is-now-available.aspx

Snoop WPFオブジェクトのバインディングなど

https://snoopwpf.codeplex.com/

このサイトが詳しいです!

http://urasandesu.blogspot.jp/2013/12/snoop-mission-in-powershellable.html

Memory Leak 調査用

http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

Resharper Binding ErrorなどをVisual Studio上で提示してくれる・・・が有料。

http://www.jetbrains.com/resharper/features/xaml_editor.html

XAMLのBinding Errorの調査に活用。

ResourceDictionary遅い

http://leecampbell.blogspot.jp/2010/05/mergeddictionaries-performance-problems.html

レンダリング

http://www.codeproject.com/Articles/784529/Solutions-for-WPF-Performance-Issue

http://kynosarges.org/WpfPerformance.html

http://blog.xamlwonderland.com/

コンバーター

http://stackoverflow.com/questions/7347911/converter-best-practice-for-better-performance

メモリリーク

https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/

画像(Image/BitmapImage)関連

http://d.hatena.ne.jp/fkmt5/20130729/1375090831

http://code.logos.com/blog/2008/04/memory_leak_with_bitmapimage_and_memorystream.html

http://blogs.msdn.com/b/llobo/archive/2009/11/10/new-wpf-features-cached-composition.aspx?wa=wsignin1.0

http://stackoverflow.com/questions/2463852/how-can-i-keep-a-wpf-image-from-blocking-if-the-imagesource-references-an-unreac

http://blogs.msdn.com/b/dditweb/archive/2007/08/22/speeding-up-image-loading-in-wpf-using-thumbnails.aspx

バインディング関連

http://stackoverflow.com/questions/7687000/fast-performing-and-thread-safe-observable-collection

https://support.microsoft.com/ja-jp/kb/938416/en-us?wa=wsignin1.0

http://stackoverflow.com/questions/160391/listbox-with-grid-as-itemspaneltemplate-produces-weird-binding-errors

特定のコントロールに特化

DataGrid

http://stackoverflow.com/questions/11379357/wpf-net-4-datagrid-column-performance

xamDataGrid

http://www.infragistics.com/community/blogs/kiril_matev/archive/2010/10/26/optimizing-xamdatagrid-performance.aspx

http://www.infragistics.com/community/forums/t/48307.aspx

http://stackoverflow.com/questions/23660485/wpf-xamdatagrid-hierarchical-load-on-demand-dynamic-columns

http://www.infragistics.com/community/forums/t/68474.aspx

http://brianlagunas.com/xamdatagriddynamically-create-and-data-bind-columns-with-an-editor-of-your-choice/

http://www.infragistics.com/community/forums/t/43169.aspx

最後の手段(ProgressBar/SplashWindow)関連

http://www.codeproject.com/Articles/35288/WPF-Circular-Progress-Bar

http://romenlaw.blogspot.jp/2008/07/wpf-splash-with-progressbar.html

http://www.codeproject.com/Articles/38291/Implement-Splash-Screen-with-WPF

http://stackoverflow.com/questions/22026209/wpf-splashscreen-with-progressbar

.NETアプリケーションの起動時間短縮方法

https://msdn.microsoft.com/ja-jp/library/cc656914%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

番外

WCF

http://blogs.msdn.com/b/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx

いやー、色々注意しないといけないですね。 本当に難しい。WPF+XAML