Should I ever manually force garbage collection in my WPF app?
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:
The whole purpose of the .NET garbage collector is to manage memory on our behalf. However, in some very rare circumstances, it may be beneficial to programmatically force a garbage collection using
GC.Collect()
. Specifically:
- When your application is about to enter into a block of code that you don't want interrupted by a possible garbage collection.
- When your application has just finished allocating an extremely large number of objects and you wish to remove as much of the acquired memory as soon as possible.
It is a good practice never force garbage collection manually, in any .NET application. GC is supposed to be smartest than us ( and actually it is smart ). Unfortunately if there is memory leak, even calling forcibly the GC does not help.
I agree with you on both sides of your point :-). I generally take the approach that the people who wrote the .NET runtime are smarter than I am and have a better understanding of garbage collection than I do, so I leave it alone.
I have seen a few instances where people thought they were doing good by forcing garbage collection, but there was never any hard evidence that it helped.
The only time I use GC.Collect is to quickly find out how much memory is ready to be collected.
Otherwise it is useless because up until now the GC is smarter than I am.
I believe you should read this article: Secrets of WPF Memory Allocation. It's not about your question at all, but is valuable for understanding WPF behavior.
While manually calling GC.Collect
is useless, you can manually call Dispose
on the bigger objects when you know you're done with them, so even if you're holding references to them in your code for no reason (sloppy), at least their destructor is called and (if written better than the aforementioned sloppy code) shouldn't actually occupy much memory at all.
精彩评论