I’ve just been sorting out some memory leaks in my WPF application. To do so, I used the CLR profiler, as well as watching process statistics in Windows Task Manager. My basic test was to make sure that when a certain window was closed, it didn’t still hang around in memory.
I’m slightly new to windows development, and at first I was getting confused because in a simple test application, it seemed as if no matter what, my windows were always staying in memory after being closed. But I eventually worked out that this did not mean there was a memory leak, but just simply that they hadn’t been garbage collected yet. So I had to create a button in my main window hooked up to an event handler that contained code to manually force garbage collection. By manually garbage collecting, I could then complete my memory leak tests, and I got it all sorted.
But it got me thinking – is there ever a need to manually force garbage collection?
It pains me to watch my application’s memory consumption go up and up as I open and close windows. Of course, eventually, garbage collection automatically runs and it all gets sorted out. But it almost seems like a good idea to manually garbage collect after these heavy windows get closed. But is there any point? I get the feeling that testing aside, we are not supposed to force garbage collection – just let the system sort it out.
Thoughts appreciated.
Thanks for the feedback guys. I’ll go along with your advice and let the system take of care of what the system was designed to take care of!
I actually have since found a good answer to my question in a book I have on the .NET framework. It says: