Ok, I've read a couple of topics about it, but here it goes. Let's imagine I have an application where basically every now and then I will click on a button, a lot of things will happen for a couple of minutes, and then it'll stay idle possible for another hour, or maybe just 1 minute. Wouldn't just after that whole ended a good situation to call GC.Collect? I mean, I do know that just at that moment I will not being using my application, and GC cannot guess it.
There's almost never a good reason to call GC.Collect()
.
In your case, there is absolutely no reason to call it. If you're idle for an hour, the GC will do its collections.
It's almost always a premature optimization to worry about calling GC.Collect before you've prototyped or built the application and tested its performance. The GC is usually very good at collecting memory at the appropriate times. It will certainly run a collection while your application is idle, especially if there is memory pressure in the system.
It is much more important that you follow good generational GC allocation practices (small objects, short usage, etc.) and you will likely get the performance characteristics you are wanting. If you still don't have the performance you need, after profiling and good design you might think about GC.Collect as a solution.
To know when to call GC.Collect()
, you'd need to know details of the specific collector involved with the runtime along with detailed information about a weak point that you can solve with the collection.
In other words, if you really know when you need to call GC.Collect()
, and it's not something you'd done poorly in other code, then you probably work for CLR Internals and can just fix the problem.
I'm hoping you're aware that calling GC.Collect doesn't cause more (or less) objects to be collected.
If you're trying to optimize for time, do you know the time GC takes to collect objects in your application? On desktop operating systems (XP, Vista etc..), the CLR uses a concurrent GC, and it can run without suspending all threads in the application for the duration of the collection.
Explicitly calling GC.Collect is not recommended because
It throws the CLR GC Tuning algorithm out of gear. The tuner determines when to trigger a GC automatically, and forcing a manual GC messes with its calculations.
By forcing a collection manually, you can end up promoting objects up a generation - objects which might have been collected in the next GC ( if they were "orphaned" before the GC decided to kick in).
You might find it interesting that .NET 4.0, a GC notification mechanism has been introduced for these kinds of scenarios.
Normally the GC is only called if you attempt to allocate new memory. If you don't run out of memory, you obtain 0% performance improvement from calling the GC. You have to have a pretty insanely resource intensive application to even come close to the limits of RAM on todays computers.
If you program has lots of external resources(like files or COM/DCOM references), you potentially would want to call the GC.
If calling the GC will give you peace of mind, then go ahead. It likely won't help, but it certainly wouldn't hurt.
Yea as mentioned in other posts GC know when to start collection better than you, the factor that no buttons are clicked in your application doesn't mean that it is the time to start cleaning, GC is doing some kind of lockings when moving the objects, so this may cause low performance if you will abuse GC.Collect
I can see that several people have gone extreme about not recommending to call GC.Collect.
GC.Collect is there for a reason, here are my recommendation of when and why to call GC.Collect.
In General, don't worry about calling it, GC tunes itself very well and will do the right thing.
Sometimes, you end up in situation where you know for sure that this is the right time to call it, the situation you described above is exactly the right time to call it, in fact Asp.Net calls GC.Collect at certain points that are similar to what you described.
The GC is smart about calling GC.Collect, if you called GC.Collect, the GC can override your decision and still doesn't collect ( you have to set a flag when you call GC.Collect to choose this behavior), this is the recommended way of calling GC.Collect, since you are still letting the GC decides if it is a good time to collect.
Don't take my recommendation is a general statement for calling GC.Collect, you should always avoid calling it, unless you REALLY sure you need to call it, situation like the one you described is exactly why GC.Collect is there.
The benefit you will get from calling it is freeing the garbage quickly, generally you will care about this situation if
- You are in low memory situation and want to be eager about collection, if you are in low memory situation, the GC will be aggressive anyway and will kick in automatically if the memory pressure is high on the machine
- If you want to avoid getting in low memory situation and you want to collect eagerly.
Hope this helps.
Thanks