Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Silverlight Controls that don't leak memory

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?).

like image 361
Stephen Ellis Avatar asked Nov 25 '10 16:11

Stephen Ellis


2 Answers

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

like image 122
Gilad Avatar answered Sep 20 '22 21:09

Gilad


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/

like image 36
DanPristupov Avatar answered Sep 21 '22 21:09

DanPristupov