Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 448
Ross Avatar asked Apr 22 '11 15:04

Ross


People also ask

When should you force garbage collection?

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.

Can garbage collection be forced manually C#?

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.

Is garbage collection Necessary?

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.

Can we force garbage collector to run?

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.


3 Answers

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.
like image 125
Ross Avatar answered Nov 15 '22 16:11

Ross


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.

like image 30
Felice Pollano Avatar answered Nov 15 '22 18:11

Felice Pollano


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.

like image 27
NateTheGreat Avatar answered Nov 15 '22 16:11

NateTheGreat