Silverlight: Clear unused memory on navigating to another page
My silverlight application has many pages and uses Navigation Service to navigate between pages. One of the pages fetches a large amount (~1G) of data from a web service. Since garbage collection is not happening automatically, I need to clear the memory allocated in that page when I go back to the previous page (Home page).
I tried overriding OnNavigatedFrom
method 开发者_JS百科and tried assigning object references to null and forcing a garbage collection by GC.Collect()
. But the memory is not getting cleared. I noticed that if I refresh the Home Page, the memory comes down drastically. How to do memory clearance programatically.
Calling CG.Collect
manually is not a good idea, and in any case GC.Collect does not help removing memory leak. Memory that can be free is free by the standard CG behaviour. Maybe looking for the reason that memory is not automatically free will evidentiate some bug somewhere.
You will need to spend a lot of time tracking the memory leak down and this can be a painful process. There are some good tools out there that can assist you.
See my previous post here: Silverlight 4 memory leaks
One way I've cleared the memory is to re-load the page, or to have the same app span across multiple asp.net pages...
So instead of navigating, it will unload the app (releasing all memory) and re-load the app. I know gurus will hate this short-sighted answer, but it's the only way I've effectively been able to have the memory dump.
An example would be say your app is in default.aspx
and instead of navigating to something like default.aspx#control1
, you put the same app in a new page like control1.aspx
and navigate to control1.aspx#control1
. Either that, or you can separate the app out into multiple apps to be loaded across multiple pages.
I've noticed huge gains from this.
精彩评论