Definition - What does Garbage Collection mean?
Garbage collection, in the context of .NET, is the process by which the common language runtime (CLR) of .NET framework manages the memory by allocating and releasing memory automatically.
Garbage collector of .NET tries to compact the memory in such a way as to increase the working space required for heap. The class GC of .NET class library controls the garbage collector. The core task of performing a collection is executed by the GC’s optimizing engine that determines the best time to perform collection based on allocations being made. GC runs are non-deterministic since the call to GC cannot be guaranteed. However, explicit calls to the overloaded 'Collect()' method of the class GC can be used whenever needed.
The advantages that GC provides include:
- the elimination of memory de-allocation code in applications
- optimized memory usage in managed heap
- clearing up of memory of reclaimed objects that are no longer in use (which helps to initialize managed objects allocated in future and provision of memory safety of objects to avoid an object using content of another.)
Techopedia explains Garbage Collection
GC manages the virtual memory on the managed heap, which is the memory segment used to store and manage objects created in a managed process. If an object does not have any reference and cannot be reached or used, it becomes garbage. While GC performs a collection in a separate thread, all unusable objects are enumerated and the memory allocated to them is reclaimed.
Garbage collection is executed in situations such as when the system having low physical memory or where memory allocated in managed heap exceeds acceptable threshold value. Since the GC is executed periodically, there is generally no need to call the GC.Collect() method.
The two options in which GC can be configured to specify the way by which the CLR need to perform are 1) workstation and, 2) server garbage collection. The key difference between the two is that the former occurs in the user thread that triggered the GC whereas the latter occurs on threads running at highest priority level. Also, workstation GC is always used on a system having single processor while the server GC is resource intensive with larger size segments and used in systems with multiple processors.
The two types of possible collections are full and partial types. A full collection is executed by stopping the program execution and visiting every object, following its object pointer and marking the object as reachable (or live) or unreachable (or condemned). After visiting the objects, the memory of the unreachable objects is reclaimed and the living objects are slided so the memory allocated is contiguous without any waste space in between. Partial collection searches only a part of heap and is used when full collection is found to be expensive.
The limitation of GC is that it does not release the unused objects that are still referenced, which can cause memory leaks.