Memory management is primary for any application. From the very beginning, we have used destructors, or deleted the allocated memory whilst using the other programming languages like C or C++. C# on the other hand being a proprietor of .NET framework provides us a new feature so that the programmer does not have to bother about the memory deallocation and the framework does it automatically. The basic usage is :

Always leave local variable.

Set class variables, events etc to null.

This leaves us with lots of queries. When does the garbage collection executes? How does it affect the current application? Can I invoke Garbage collection when my system / application is idle ?

These questions might come to any developer who has just came to .NET environment. As for me too, I was doing the application just blindly taking it account that this might be the basic usage of .NET applications, and there might be a hidden hand for me who works for me in background. Until after few days, I got an alternative to call the Garbage collection using

GC.Collect()

But according to the documentation, GC.Collect() is a request.It is not guaranteed that the Collection process will start after calling the method and the memory is reclaim So, there is still uncertainty whether actually the Garbage Collection will occur or not.

Why GC.Collect is discouraged ?

GC.Collect actually forces the Garbage collection to invoke its collection process out of its regular cycle. It potentially decreases the performance of the application which calls it. As GC.Collect runs in the thread on which it is called, it starts and quickly finishes the call. In such phase of GC collection, actually the memory is not been reclaimed, rather it just produces a thorough scan on objects that are out of scope. The memory ultimately freed whenever Full Garbage collection takes place.
For reference you can read Scotts blog entry.

Whats new in .NET 4.0 (or .NET 3.5 SP1)Well, GC is changed a bit with the introduction of .NET 4.0, so that the programmer have better flexibility on how to use GC. One of such changes is the signature of GC.Collect()

Initially, GC.Collect only allows you to use Optimization call. Now it has 3 overloads :

GC.Collect(int) : You pass the Generation 0 to specified generation to collect all generation starting from 0.

GC.Collect(int, GCCollectionMode) : You can also specify the CollectionMode in addition to it. The CollectionMode can be CollectionMode.Force, CollectionMode.Optimized, and Default which is Forced.

GC.Collect is Forced by default, so once you call it, it will automatically invoke the collection process.
So its a great relief to all of you right?

GC Notifications

Another feature that is introduced with GC in .NET 3.5 SP 1 is to produce notifications whenever GC collection is about to begin and GC collection is completed successfully. Say you are between a very resource consuming phase of your application, GC notification will allow you to get notified that GC is approaching, so that you could stop the current process and wait for GC to complete. This makes your application to run smoothly. So how can you do this ? Lets look at the steps :

Call GC.RegisterForFullGCNotification to allow for notifications when GC is approaching.

Create a new thread from the application and start poll continuously in an infinite loop to methods GC.WaitForFullGCApproach and/or GC.WaitForFullGCComplete methods.

Both the method returns GCNotificationStatus.Succeeded when the notification has to be raised.

In Calling thread use GC.CancelFullGCNorification to unregister the notification process.

Here when the program starts I have called RegisterForFullGCNotification. This call makes registers the GC to start notifying using the notifiers. Please note that the call to RegisterForFullGCNotification will fail if a concurrent GC is enabled. You will see "This API is not available when the concurrent GC is enabled.". Concurrent GC means the garbage collection will run in separate threads concurrently. If your application has to do some heavy task, leave concurrent GC enabled. This makes the GC run collection without interrupting the applicaition thread. For Asp.net application, you should disable concurrentGC.

After concurrent GC is disabled you can run the application. After calling RegisterForFullGCNotification, we start a new thread. In the thread we continuously polling WaitForFullGCApproach which returns GCNotificationStatus.Succeeded whenever the GC is approaching. It is better to call GC.Collect in this situation to ensure that collection starts immediately.

We also poll WaitForFullGCComplete which returns GCNotificationStatus.Succeeded whenever the GC collection is complete.

We use GC.CancelFullGCNotification() to stop using Notiification.

From the main thread, we continuously allocating memory to check when GC collection occurs. The GC.CollectionCount to determine how many GC collection took place at a given instant of time. Use 0 - 2 to check each generation individually.

Blog Subscription

Learn MVC 5 step by step

My friend Shivprasad Koirala who is also a Microsoft ASP.NET MVP has released Learn MVC 5 step by step video series. It starts right from basics of MVC and goes to a level until you become a professional.
You can start taking the course for free using the below youtube video.
Please try it, you will find it awesome.

My Awards

Hit Counter

Twitter

Best .NET 4.5 Expert CookBook

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!
Another book on .NET 4.5 has been released very recently. It covers Debugging, Testing, Extensibility, WCF, Windows Phone, Windows Azure and many more...
Grab it now !!!GET ANY BOOK AT $5 from PacktPub. Offer is limited