Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GC.Collect()

Tags:

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.

like image 607
devoured elysium Avatar asked Jul 19 '09 03:07

devoured elysium


People also ask

What does GC collect () do?

It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.

What is GC collect () Python?

Python deletes unwanted objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically frees and reclaims blocks of memory that no longer are in use is called Garbage Collection.

When GC collect is called?

Collect. The C# language is a garbage-collected language. This means that memory that is no longer referenced by your program will be reclaimed and is later reused.

Should we call GC collect?

The general advice is that you should not call GC.


1 Answers

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.

  1. In General, don't worry about calling it, GC tunes itself very well and will do the right thing.

  2. 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.

  3. 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.

  4. 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.

  5. The benefit you will get from calling it is freeing the garbage quickly, generally you will care about this situation if

    1. 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
    2. If you want to avoid getting in low memory situation and you want to collect eagerly.

Hope this helps.
Thanks

like image 162
mfawzymkh Avatar answered Oct 17 '22 08:10

mfawzymkh