views:

179

answers:

4

Due to the overhead of calling the garbage collector in the CLR, is it more efficient to leave it, or force to garbage collection when objects go out of scope?

+4  A: 

Most of the time, it's not worth it to manually call the garbage collector. You would be surprised at how much efficient the underlying garbage collector algorithm is.

Unless if you are working in financial application (e.g: derivative trading application) that requires you to have full control of the time when garbage collector gets called. But if you are going into that route maybe you should try other language, such as C++

Ngu Soon Hui
Why would a financial application need this?
Todd Stout
If you're trading securities for example, with very precise timing requirements, GC may impact that negatively when you do not want it to.
Joe
Explicitly calling GC guarantees nothing. If you have hard real-time requirements, then as you point out in your edit, perhaps managed code is not a good fit. However, if you really have real-time constraints, then C++ won't help either unless the underlying OS has latency guarantees, which Windows does not.
Todd Stout
+2  A: 

The people that wrote .NET and the Garbage Collector put a lot of thought into it so you don't have to. Just call dispose on your objects when applicable and don't worry about the rest.

Bob
+6  A: 

One of the main reasons not to call the GC yourself is that since the GC is self-tuning, any calls you make to collect from the managed heap will effectively destroy any of the self-tuning that the GC has done on your application's behalf.

One of the main things that the GC will do is adjust the thresholds that trigger garbage collection on each of the three generations of the managed heap to reduce the number of collections that are needed for your application.

For all but extreme cases it is best to allow the GC to do its job and not worry about invoking it manually. Only when you have identified a clear need should you manually invoke a collection.

Andrew Hare
+4  A: 

In addition to Andrew's comments, unnecessarily calling GC.Collect will cause otherwise efficient Gen-0 collections to be pushed into Gen-1 and Gen-2 buckets which are collected far less frequently. Ironically, this means your application will suffer from increased longer-term memory usage, defeating the original intention of reducing memory usage.

One of the few cases where calling GC.Collect is necessary would be when interoping with COM or Win32. For example, it's not uncommon for COM objects in Office to hang around longer than intended because there is no way to explicitly dispose them. Therefore you might want the COM wrappers to be GC'd at a specific time and usually that would be followed up with a call to WaitForPendingFinalizers.

Josh Einstein
when the GC runs does it just collect from GEN-0, until its full then run on GEN-1?
Euclid
It performs Gen-0 collections more frequently. I believe it performs Gen-1 and Gen-2 collections based on the memory pressure situation. Here's a good MSDN article about the algorithm. http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
Josh Einstein
And part 2... http://msdn.microsoft.com/en-us/magazine/bb985011.aspx
Josh Einstein