Wednesday 15 October 2014

Server GC in .Net Framework 4.5

.NET Garbage Collection

Once memory allocation is done, it is required to deal locate the memory once that object goes out of scope or no longer been used in future. It is highly recommended to do memory management to reduce the cases of memory leak and it is automatically controlled by garbage collector in .Net framework.

Manual Memory Management

When you use any unmanaged language or frameworks, you are required to allocate and deallocate the memory of object created manually. In manual process of memory management you are required to write a command to allocate memory from environment and again you have to intimate environment to release the memory of object which are no longer been used in future.

Manual memory management is quite foggy and due to its drawback, .Net introduce automatic memory management algorithm which is known as garbage collection.

Automatic Memory Management (Garbage Collection)

The key aspect of .Net frameworks is developer does not need to allocate or deallocate memory manually and let the developer concentrate on actual coding to make good outcome of software instead of bothering and thinking about the memory allocation. And in most cases developer doesn't need to think much about memory management aspect.

When you create the object, framework allocates memory automatically and when object no longer used, the common language runtime (CLR) deals with the deallocation of the memory that they used.

The garbage collection process is complex with many optimizations. And still microsoft is optimizing it more and more with new release of .Net frameworks.

In recent release of .Net framework 4.5, GC comes with more improvement and addition of Server GC.

GC in Framework 4.0

In .NET 4.0, when the GC runs for cleanup, all the application threads are suspended. The different application threads are running for different application and GC is running on separate thread. Each logical processor will have their separate thread. Each application create their managed objects and as GC run on separate thread, that also create their managed objects. At some point of time the background process for garbage collection will run and release the objects memory which are no longer been used in future but as garbage collector itself create their own managed object it has to manage those objects as well which slow down the server/application for the moment.

To overcome the this problem, server GC was introduced.

Server GC in Framework 4.5

Server GC is one more thread created which runs in the background which manage the object created by main GC and it reduce the load of main GC thread. thus increasing application throughput.

How to enable server GC

To enable server GC, we need to use the gcServer XML tag and enable it to true.

<configuration>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
</configuration>

Read more here : Async and Await in .Net Framework 4.5