Memory Management and Garbage Collection in .NET

.NET supports two kind of codingcode

  • Managed Code

  • Unmanaged Code

Unmanaged Code

The code, which is developed outside .NET Framework, is known as unmanaged code.

Cleaning Up Unmanaged Resources

The most common types of unmanaged resource are objects that wrap operating system resources, such as files, windows, network connections, or database connections.Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it doesn’t know how to release and clean up the unmanaged resource.

When you create objects that include unmanaged resources, you must explicitly release those resources when you finish using them in your app.

There are two ways to do this:

  • Use a safe handle to wrap your unmanaged resource.System.Runtime.InteropServices.SafeHandle class handler include Finalize method.Simply implement the IDisposable interface and call Dispose method in your IDisposable.The handler finalizer is called automatically by the garbage collector if its Dispose method is not called.
  • Override the Object.Finalize method. Finalization enables the non-deterministic release of unmanaged resources when the consumer of a type fails to call IDisposable.Dispose to dispose of them deterministically.

Managed Code

The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution.

Cleaning Up Managed Resources

Garbage Collection

.NET’s garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector’s optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

Garbage Collection Benefits

  • Enables you to develop your application without having to free memory.
  • Allocates objects on the managed heap efficiently.
  • Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.
  • Provides memory safety by making sure that an object cannot use the content of another object.

Conditions for a garbage collection

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

  • The system has low physical memory. This is detected by either the low memory notification from the OS or low memory indicated by the host.
  • 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.
  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Algorithm for a garbage collection(Mark,Relocate,Compact)

  • A marking phase that finds and creates a list of all live objects.
  • A relocating phase that updates the references to the objects that will be compacted.

  • A compacting phase that reclaims the space occupied by the dead objects and compacts the surviving objects.


  • Generation 0. This is the youngest generation and contains short-lived objects.Ex :  temporary variable
  • Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
  • Generation 2. This generation contains long-lived objects.Ex:Static data.





Leave a Reply

Your email address will not be published. Required fields are marked *