I've just been investigating some Silverlight controls with ANTS Memory Profiler (brilliant!) and found out that some of my controls are staying around in memory. The solution to this has been to reset the Template and Content values to null when control 'Unloads' and to clear any Bindings I've done in code.
Is this the normal pattern for dealing with Silverlight custom controls and have I just failed to do this properly in the past?
Does anyone know of a paradigm e.g. of the pattern for writing Silverlight controls with tidy clean-up?
Addendum I've been investigating this further and found that Bindings to the DataContext are removed when the DataContext is set to null. It seems that the correct procedure is to set any Datacontext value in the Loaded event and set it to null in the Unloaded event. I've still got a fundamental problem with the paradigm for writing Silverlight Templated controls as I just can't force ContentControls to be destroyed (see my issue here: Why are Silverlight ContentControls not garbage collected?).
MS has released Silverlight 4 GDR3 that includes memory leak fix's I'm not sure if this fixes your case but it is worth downloading the latest SL version and testing again.
http://timheuer.com/blog/archive/2011/02/14/silverlight-february-2011-update-gdr3.aspx
Did you recognize where is leak? Is it in your or in MS code?
Here is good post about leak detecting in SL: http://dotnetspeak.com/index.php/2010/09/using-windbg-to-find-memory-leaks-in-silverlight-applications/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With