Garbage Collection In .NET 4.5

In this article you will learn the new features in .NET 4.5.


I have read this in many blogs, but all are complicated. My effort here is to give a introduction to this new feature in .NET 4.5 so that understanding will be easier.

A look through what MSDN has to say,

"The .NET Framework's garbage collector manages the allocation and release of memory for your application. 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."

Each of us has heard about managed and unmanaged code. What is a managed code? OOPS basics, this is related to automatic memory management by Garbage Collection, so called GC.

memory management

GC in .Net is a very heavy process; this is improved in .NET 4.5. These improvements are intended to provide value for developers who build large-scale client and server apps and many smaller apps, as well.

Garbage Collection till .NET 4.0

But GC had many challenges in the older versions of .NET. Larger the application is, more the resources it will consume. ASP.NET applications run on the server and a lot of clients send requests to the server thus creating loads of objects, making the GC really work hard for cleaning up unwanted objects. This caused larger pause for the application lowering the efficiency, the large object heap takes up more space.

To be clearer, till the earlier version of .NET, i.e. .NET 4.0, when there are different threads doing their work and have managed code as well, at some point of time the GC runs creating a pause in the application suspending all the application threads, making the server less responsive.

What’s new in .NET 4.5 GC?

.NET 4.5 has overcomed this problem, by introducing server GC. This creates one more thread in the background cleaning the objects. This handles only Gen 2 objects, reducing the load on main GC thread. The two threads running simultaneously reduce the suspension time and improve the application throughput.

new in .Net 4.5 GC

Enabling GC server

The GC server can be enabled by using GCServer XML tag in the web.config and enable it to true.

  1. <configuration>  
  2.    <runtime>  
  3.       <gcServer enabled="true"/>  
  4.    </runtime>  
  5. </configuration>