Garbage collection

Garbage collection is a functionality, provided by CLR in .NET, which helps us to clean up the memory occupied by managed objects. It is a thread that executes in the .NET Framework and, at regular intervals, checks whether there is any unused memory in the application. If it does find memory, then it reclaims that memory and destroys the underlying object. 

Suppose we have implemented a .NET web application in C#. Now, let's assume that during any interval of time, there are several people who are trying to access this .NET application. The following is one particular scenario that will give us an idea of why garbage collection is a very important part of C# or, for that matter, any .NET application:

  • When a user browses the application, they can execute a number of functionalities, such as accessing their profile or executing operations (for example, creating, updating, and deleting information).
  • This information can be stored in different sources such as SQL, Oracle, or more.
  • To access this information and to execute these operations, the application will require the creation of different objects during the application runtime.
  • Assuming a scenario where memory is just being allocated to different objects but is not being cleaned up, over the course of time we will end up with a system that has too much unused memory. Memory cleanup is logical when the object declared in the memory is no longer required. For example, suppose that a user, after performing the intended operations in the application, logs out. In this case, the memory that was allocated for the operations of that particular user is no longer required. Therefore, that memory can be reclaimed.
  • A the memory allocated to the application could be limited, this will lead to performance degradation over time. 

Garbage collection in the .NET Framework ensures that such situations never arise for managed code. This thread runs in the background of the application and, at set intervals, reclaims the memory. 

Please note that garbage collection can only reclaim the unused memory of managed code. For unmanaged code, which we will learn about later, we need to explicitly write code to ensure that no memory leaks occur in the application.

The garbage collector in .NET executes the following tasks in an application:

  • Allocation of memory: Each application running on .NET maintains a memory block required for its execution in a managed heap. The garbage collection manages the allocation of memory from this heap structure to the objects used in the program. In upcoming sections, we will learn more about managed heaps.
  • Deallocation of memory: The garbage collector runs at set time periods during the application runtime and looks for objects that are no longer required by the application. It then destroys those objects and reclaims the memory for future use.
    The garbage collector reclaims the memory when one of the following three conditions occurs during the execution of a program:
    • The application has low memory: Each application running in .NET requires memory for its successful execution. If CLR determines that the application is getting free low memory from the OS, it tells the garbage collector to free any unused memory.
    • The relocation of memory: Garbage collection in C# is based on generations. Generations are simply divisions in the managed heap used by the application. In C#, we can have three generations: generation 0, generation 1, and generation 2. In upcoming sections, we will learn how generations are classified. The garbage collector tries to optimize the performance of the system by classifying the objects used in the application among the three generations of a managed heap. In generation 0, it keeps the newly created objects in the application run. In comparison, in successive runs it identifies the objects that are being used for a longer period in the application execution. It classifies them as generation 1 and generation 2 and then loops through these generations less extensively than it does for generation 0. This, therefore, results in better performance.
    • When the Collect method is called: As programmers, we hardly need to call the garbage collector method explicitly, as .NET is smart enough to ensure that garbage collection occurs at regular intervals. However, there could be certain scenarios where we would need to call this method explicitly. In such cases, we can do it by calling the GC.Collect method. In this chapter, we will look at a program implementation in which we do this.

Now, let's go through some of the basic structures that garbage collection works with in C#. We will start with a managed heap, which we will explore in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.3