Generations and the garbage collection process
Since the CLR handles the automatic creation and allocation of objects in memory, its most trusted companion is the automatic GC. The GC monitors objects in memory and decides whether they should stay longer based on the program’s needs or if they should be destroyed from memory to be reclaimed.
This automatic memory management mechanism eliminates common developer problems, such as the following:
- Forgetting to free an object, which can lead to a memory leak and poor use of system resources
- Attempting to access an object that has already been removed from memory
The .NET runtime has a built-in garbage collection engine orchestrated by the CLR. The CLR employs a garbage collection strategy to ensure that available memory is only occupied by objects still useful for the current processes. This mechanism is called the GC.
When a new process is initialized, the CLR reserves a contiguous space in memory called...