Stephen Ellis
Stephen Ellis

Reputation: 2711

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

Upvotes: 7

Views: 1229

Answers (3)

Stephen Ellis
Stephen Ellis

Reputation: 2711

My problem appears to be associated with my desire to have re-loadable controls. I've asked the question again in more detail here: Should Silverlight Controls be re-loaded onto pages?

Upvotes: 0

DanPristupov
DanPristupov

Reputation: 11

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/

Upvotes: 1

Gilad
Gilad

Reputation: 2886

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

Upvotes: 2

Related Questions