Milind Chavan
Milind Chavan

Reputation: 115

Memory leaks in wpf C#

I am developing a WPF MVVM (in v3.5) application using GalaSoft.MvvmLight suite and WPF Toolkit for DataGrid and DatePicker. However I am facing lots of memory leaks in the application every time I load a UserControl in MainWindow. I am trying to get the exact issue via Scitech Memory profiler, though I have some serious questions :

  1. How to unhook/unbind the RelayCommand from the Event in VM?
  2. mscorlib and PresentationFramework are also consuming lots of memory and most of them are strings. I don't know where they are coming from.

Please help.

Upvotes: 2

Views: 3226

Answers (2)

akjoshi
akjoshi

Reputation: 15802

Have a look at these articles to know causes of memory leaks in WPF, there solutions and how to find them -

Finding Memory Leaks in WPF-based applications:

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

Memory Leaks in WPF applications:

http://svetoslavsavov.blogspot.com/2010/05/memory-leaks-in-wpf-applications.html

Finding Managed Memory Leaks in WPF and Silverlight applications:

http://blogs.msdn.com/b/wpftesting/archive/2009/03/21/finding-managed-memory-leaks-in-wpf-and-silverlight-applications.aspx

In case you are using WPF 3.5 SP1, have a look at these hot-fixes -

Memory Leak Hotfixes for WPF 3.5 SP1

http://blogs.msdn.com/b/jgoldb/archive/2010/06/14/memory-leak-hotfixes-for-wpf-3-5-sp1.aspx

Some more rare scenarios causing memory leaks -

http://blog.ramondeklein.nl/?p=58

http://code.logos.com/blog/2008/10/detecting_bindings_that_should_be_onetime.html

Upvotes: 3

ColinE
ColinE

Reputation: 70150

Are you certain that these are memory leaks? are you using the profiler correctly? To confirm that you have a memory leaks you must run the garbage collector so that all the objects that are not references by a stackframe are deleted. Once you have run the garbage collector inspect the heap memory for objects which should have been collected.

E.g. in your application, open a view or document, close it, garbage collector, and then check to see if it has been removed.

Upvotes: 2

Related Questions