Monday, May 21, 2007

Garbage Collection

Define Garbage Collection?
The garbage collector is .NET’s answer to memory management, and in particular to the question of what to do about reclaiming memory that running application ask for. Up until now two techniques have been used on the Windows platform for deallocating memory that processes have dynamically requested from the system.
· Make the application code do it all manually.
· Make objects maintain reference counts.

When should you call the garbage collector in .NET?
As a good rule, you should not call the garbage collector. However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory. However, this is usually not a good practice.

Is there a way to force garbage collection?
Yes, set all references to null and then call System.GC.Collect(). If you need to have some objects destructed, and System.GC.Collect() doesn’t seem to be doing it for you, you can force finalizers to be run by setting all the references to the object to null and then calling System.GC.RunFinalizers().

No comments: