|
In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory used by objects that will never be accessed or mutated again by the application.
The .NET Framework is one of the many programming languages that include a garbage collector which runs periodically, on a separate thread from the application's thread, that enumerates all the unusable objects and reclaims the memory allocated to them.
The .NET Garbage Collector (GC) is a non-deterministic, compacting, mark-and-sweep garbage collector. The GC runs only when a certain amount of memory has been used or there is enough pressure for memory on the system. Learn more of this with the austin .net consultant.
Each .NET application has a set of roots, which are pointers to objects on the managed heap. These include references to static objects and objects defined as local variables or method parameters currently in scope, as well as objects referred to by CPU registers.
When the GC runs, it pauses the application, and for each object referred to in the root, it recursively enumerates all the objects reachable from the root objects and marks them as reachable. It uses .NET metadata and reflection to discover the objects encapsulated by an object, and then recursively walk them.
It then enumerates all the objects on the heap using reflection. All objects not marked as reachable are garbage. This is the mark phase. Since the memory held by garbage is not of any consequence, it is considered free space.
However, this leaves chunks of free space between objects which were initially contiguous. The objects are then compacted together, by using memcpy to copy them over to the free space to make them contiguous again. Visit the austin .net consultant to learn more about garbage collection in the .NET Framework.
The GC used by .NET Framework is actually generational. Generational garbage collection is a heuristic approach, and some unreachable objects may not be reclaimed on each cycle. It may therefore occasionally be necessary to perform a full mark and sweep or copying garbage collection to reclaim all available space.
In fact, runtime systems for modern programming languages, such as the .NET Framework, usually use some hybrid of the various strategies that have been described thus far; for example, most collection cycles might look only at a few generations, while occasionally a mark-and-sweep is performed, and even more rarely a full copying is performed to combat fragmentation. For more information regarding garbage collection in the .NET Framework, then visit the austin .net consultant for more details.
www.astonishdesigns.com
|
|