.NET GC is Excellent -- Better Than Java

I've received a couple of emails lately due to my postings where I've apparently made some people worried about .NET.  I want to make it very clear that I am very very impressed with the .NET Garbage Collector -- there is no need to be “worried”.  I've built enough systems, both before .NET and with .NET, to have experienced the blessings of the .NET GC -- they are real.  I have yet to build an application in .NET that leaked any memory in the old sense that it was not even reclaimable after the app was terminated, except in cases where a 3rd party control had leaks due to unmanaged code.  Yes, there are a few cases I've seen where .NET will leak memory, but only in the newer sense that your application's footprint keeps growing while it is still running -- it was always freed upon termination which means the GC did work.  Its also possible to have these types of leaks in your own code -- we found one ourselves today where we had an object chain that included an object that was included in a static collection.  I've also been very frustrated that the experts, including Microsoft people, have made simplistic statements that are just wrong in some cases -- one person that emailed me told me that the MS consultants at his site assured him that I was wrong about setting objects to null.  I'm also very frustrated that we actually have to keep refinding the leaks in the framework that have been found before, but which Microsoft still does not list anywhere on their own site -- not even in the partner level KB that I have access to as an MVP.  But none of this means that the .NET Garbage Collector is flawed -- its not -- it works very well, although it makes some assumptions that we developers need to understand so that we can work with it, or even help it in some cases.  Its also true that GC in general is lazy, so there are probably some systems that should not be based on such systems, although those are probably few and far between (although maybe I have one).

Now I also titled this post “Better Than Java” -- why?  I also had a couple of people email me that Java didn't have these problems -- specifically they said they didn't have to worry about Dispose methods, or setting objects to null, or even memory leaks of any kind!  Well all I can say is that anyone making such claims is living in a fantasy world, and is hopefully only writing small department applications instead of large enterprise systems.  Its actually quite easy to google on things like Garbage Collect, leak, Dispose, null, or other similar terms, along with Java, and find all sorts of similar issues that Java has.  Neither Java nor .NET developers “need” to set objects to null, but both have situations where it can help.  Java also has leaks in some of its framework, and its possible for Java developers to create their own, just like .NET -- they also go away when the application is terminated.  As for the Dispose method, I learned that this pattern is commonly used and accepted as necessary in Java, but it was never officially made part of Java itself, which is one place .NET is actually better since they learned from Java's experience.  Many Java frameworks do have the dispose method, but its simply not defined in Java itself, so you can imagine there are lots of lesser experienced developers that don't realize how important it is.  To me this just underscores the importance of calling Dispose when its defined, whether we think it does anything or not -- it was added for a reason and Java is severely lacking it as a standard.  Another place where .NET is better than Java, as far as the GC is concerned, is that .NET defines the basics of the GC once and for all, even though various implementations (Rotor, Mono, DotGnu) of the framework might have slightly different internal implementations of the GC.  Java doesn't have any GC standard at all -- it simply says that it should be garbage collected -- which makes it rather difficult to properly understand or help the GC in memory critical systems -- so much for cross platform.

So please understand that I may point out issues and concerns and frustrations with .NET and its GC, but that should not be taken as a condemnation.  It simply reflects that we need to understand more about the GC and the other internals of the framework we use in order to develop anything but the smallest of apps.  And don't even mention my posts as a reason to use Java -- that's ridiculous.


Comments have been disabled for this content.