Friday, June 17, 2011

Garbage Collection in .NET

Garbage Collection in .NET: "

After almost a year that I was away from the .NET community, you would have noticed that I’ve returned recently with some new activities, and after upgrading my blog engine, writing some blog posts, and reactivating some open source projects on .NET, I reloaded Waegis. During the same time that I had taken off from the research work for a week, I worked on articles for the .NET communities that I used to contribute to before, and the outcome was republishing one of my blog posts about the performance impacts of using DateTime.Now on ASP Alliance, and also writing a separate article for DotNetSlackers entitled Garbage Collection in .NET that is published today.

Garbage Collection is one of the most important and vital aspects of compiler design that has a direct impact on the performance and speed of execution for a program. The .NET Framework has a very powerful and sophisticated garbage collector in its virtual machine that is not discovered very well, and it didn’t have any major changes since the initial release of version 1.0 of the framework until the most recent version, 4.0, where Background Collector was added to improve the quality of garbage collection. In this article the principles of garbage collection are reviewed, fundamentals of garbage collection in .NET are discovered, and the changes to garbage collector in .NET Framework 4.0 are discussed.

Starting a Ph.D. program in Computer Science by primarily specializing in the field of Programming Languages and Compilers, I learned a lot about compiler construction and the principles associated with it. One of things that I noticed in the past 1.5 years of this program was that many software developers who even work at a professional level and apply a compiler, don’t have a very solid understanding of the features of the compiler that they’re using everyday, and this weakness is more severe on the .NET community where the use of development tools and IDEs has automated a big portion of the process that is going on behind the scenes. Although it’s not reasonable to expect everyone to be a compiler expert, it’s very important to know some principles about the compiler that you’re using because such a knowledge can help you build programs with a higher performance and less resource usage.

One of these key aspects of compiler that a programmer should be familiar with to some extent is the garbage collection. Almost all the compilers that are being actively used in the world are applying a garbage collector to collect unnecessary objects being allocated on the heap to free the memory, and this part of a compiler has an important impact on its performance with ongoing research both at academia and industry to enhance it. The .NET Framework is no exception to this rule and it applies a sophisticated garbage collector to keep its managed heap in a good state available for object allocation when necessary.

But in this article I walk through the principles of garbage collection in .NET. First, I give some background information about garbage collection in compilers and its main challenges, then talk about how it’s applied in the .NET Framework with an emphasis on explaining the generational collection. I also describe the differences between workstation and server garbage collection, and end the article by describing the background garbage collection in .NET 4.0 that was a major addition to the framework since its first release.

It’s worth noting that this article emphasizes on the principles of garbage collection in .NET rather than how it’s implemented and the APIs associated with it, but I may write a separate article or blog post about garbage collection APIs in the near future. The .NET compiler is designed in a way that it hides many complexities of garbage collection from a programmer and as long as you’re not going to use an unmanaged resource in your code, you wouldn’t need to deal with garbage collection APIs in .NET. However, knowing these principles can help you understand the behavior of your applications and their resource usage, and how to write codes that utilize system resources in an optimal way.

You can read this article on DotNetSlackers, ask questions, or leave comments.

"

No comments: