views:

1980

answers:

2

I have an image control that displays a large jpg file and takes around 50mb of memory. How do I dispose of this control when it is no longer needed? I tried removing it from the parent container, setting it to null and chaning its Source property to null, but the memory usage still stays the same... Any suggestions will be highly appreciated!

A: 

I think this is an edge case problem with Silverlight due to the reliance on garbage collection. Unfortunately if you are instantiating a lot of objects (and I do mean a lot) then you will get performance degredation (even if you free everything up properly and/or Dispose() of everything) because the GC just doesn't keep up and we don't have the resources to throw around like we do in a "normal" application.

Are you getting an issue, or just memory watching?

Steven Robbins
Currently, I have basically only very few objects, and only one of them is Image control. I'm just trying to find some way to dispose of it when it is no longer needed to free up some memory resources. Large images really take a lot of memory.
The GC will sort it out. It won't be immediate, but if you're not hammering things it should keep on top of things for you.
Steven Robbins
GC should sort it out, but it doesn't.I have waited for about 15 minutes and the memory usage didn't change at all.
Then either it doesn't think it's necessary (the exact algorithm is a "trade secret" of MS I believe), or you've left a reference to it somewhere so it's not available to the GC.
Steven Robbins
I have finally found the answer to my question, please read the answer below. Thanks for the help!
+4  A: 

I have finally found the answer to my question. The problem was in a memory leak bug in Silverlight. I found the workaround for this problem here: http://blogs.msdn.com/silverlight_sdk/pages/silverlight-bugs-and-workarounds.aspx

Memory Leak when you Dynamically add and remove Images

Workaround: When dynamically adding or removing BitmapImages from an application (a.k.a. adding/removing from the tree), you should set Image.Source = null before removing the Image element from the tree. This will make the BitmapImage eligible for garbage collection. Bug Status: Active bug. *

Thanks everyone for the suggestions!

Cool, told you there was still a reference somewhere ;-)
Steven Robbins