- Garbage collection is the process of automatically de-allocating memory based on the program’s needs.
- This is a significant programming problem for languages that don’t have an automated system for doing this.
- Without the garbage collector, programmers must remember to always free any memory allocations they make.
- Forgetting to do so, or doing so repeatedly for the same memory allocation, introduces memory leaks or corruption into the program.
- Because of the runtime’s built-in support for garbage collection, programmers targeting runtime execution can focus on adding program features rather than “plumbing” related to memory management.
- GC only takes responsibility for handling memory management.
- It does not provide an automated system for managing resources unrelated to memory.
- Therefore, if an explicit action to free a resource (other than memory) is required, programmers using that resource should utilize special CLI-compatible programming patterns that will aid in the cleanup of those resources.
- The .NET platform implementation of garbage collection uses a generational, compacting, mark-and-sweep-based algorithm.
- It is generational because objects that have lived for only a short period will be cleaned up sooner than objects that have already survived GC sweeps because they were still in use.
- This conforms to the general pattern of memory allocation that objects that have been around longer will continue to outlive objects that have only recently been instantiated.
- Additionally, the .NET garbage collector uses a mark-and-sweep algorithm.
- During each garbage collection execution, it marks objects that are to be de-allocated and compacts together the objects that remain so that there is no “dirty” space between them.
- The use of compression to fill in the space left by de-allocated objects often results in faster instantiation of new objects (than with unmanaged code), because it is not necessary to search through memory to locate space for a new allocation.
- This also decreases the chance of paging because more objects are located in the same page, which improves performance as well.
- The garbage collector takes into consideration the resources on the machine and the demand on those resources at execution time.
- For example, if memory on the computer is still largely untapped, the garbage collector is less likely to run and take time to clean up those resources.
- This is an optimization rarely taken by platforms and languages that are not based on garbage collection.
- GC is the process of automatically de-allocating memory based on the program’s needs and is a core function of the “runtime”.
- Unnecessary memory de-allocation is a significant programming problem for languages because without the GC, programmers must remember to always free any memory allocations they make.
- Forgetting this or release the memory repeatedly for the same memory allocation causes memory leaks or corruption into the program.
- GC purpose is to restore memory consumed by objects that are no longer referenced.
- The emphasis in this statement lies with memory and references.
- The GC determines what to clean up based on whether any references remain.