简体   繁体   中英

Silverlight: How to unload (dispose) an image from memory?

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!

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 (aka 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!

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM