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.
Common triggers for garbage collection are Eden space being full, not enough free space to allocate an object, external resources like System. gc(), tools like jmap or not enough free space to create an object.
You can force garbage collection either to all the three generations or to a specific generation using the GC. Collect() method. The GC. Collect() method is overloaded -- you can call it without any parameters or even by passing the generation number you would like to the garbage collector to collect.
Garbage collection ensures that a program does not exceed its memory quota or reach a point that it can no longer function. It also frees up developers from having to manually manage a program's memory, which, in turn, reduces the potential for memory-related bugs.
While a developer can never actually force Java garbage collection, there are ways to make the JVM prioritize memory management functions. In review, five ways to try and force Java garbage collection are: Call the System. gc() command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With