Introduction to Garbage Collection in C#:

Memory management is the main concern for any application whether the application is window based or web-based. In .NET Framework, CLR has a garbage collector that executes as a part of our program and responsible for reclaiming the memory of no longer used objects.

The Garbage collection is a very important method.It provides memory is safe in the program and it by making sure that one object cannot use the content of another object in the same method.Garbage collector free the memory for objects that are no longer referenced and keeps the memory for future allocations.

Free PDF Download Link: Latest Updated ASP.NET MVC Training Topics

Key Points about C# Garbage collector :

  • All objects in the heap are allocated from one continuous range of memory address and heap is divided into generations. so that it is easy to eliminate the garbage objects by looking at only a small fraction of the heap.
  • The allocation pointer for the new objects marks the boundary between the allocated and free memory.
  • The newest objects are created at higher memory address while oldest memory objects are at lowest memory address within the heap.
  • The order of objects in memory remains the same as they were created.
  • Only some of the free memory is committed when required and more memory is acquired from the OS in the reserved address range.

Trending Now: C# 7.0 Features

What happens during garbage collection in C#?

Marking Phase :

Marking marking phase it finds and creates the list of lived objects.

Relocating phase :

Relocating phase it updates the references to the objects which will be compacted

Compacting phase :

Compacting phase release the memory which is occupied by the dead objects and compacts the surviving objects.

 Garbage collection  Condition in C#:

In C# Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

Advantages of C# Garbage Collector :

  • It allows developing an application without worrying to free memory.
  • It provides security of memory by ensuring that an object cannot use the content of another object
  • It allocates the memory for objects efficiently on the managed heap.
  • It releases the memory of the no longer used objects and reallocates the free memory for future allocations.

Let us have an example to know more about garbage collection:

using System.IO;

using System;

class GCmax

{

public static void Main(string[] args)

{

try

{

Console.WriteLine (“GC maximum generations:” + GC.MaxGeneration);

}

catch (Exception oEx)

{

Console.WriteLine (“Error..” + oEx.Message);

}

}

}

OUTPUT

    GC maximum generations: 1

Conclusion:

The  C#  garbage collector provides a high-speed allocation service with good use of memory and no long-term fragmentation problems, however, it is possible to do things that will give you much less than optimal performance.

Tagged with: Garbage collector in C# | Garbage Collection C# Example | C# Garbage Collection | .NET Tutorials for Fresher and Experienced | .NET Training in Chennai | Best .NET Training in Chennai