Logo




Subscribe:
RSS 2.0 | Atom 1.0
Categories:

Sign In


[Giagnocavo]Michael::Write()

 Thursday, February 05, 2004
Finalize and Dispose: Performance

In .NET, some people have found Dispose and Finalize to be a bit confusing, especially in regards to when you need to implement Finalize, and when to Dispose.

To quickly summarize, finalizers in .NET provide a way for the runtime to clean up unmanaged resources that the Garbage Collector knows nothing about.  By providing a finalizer on a class, the runtime puts the object on the finalize queue instead of collecting it the first time around.  Only after the object's finalizer runs can the GC truly free the memory.

The impact of this is that objects that are left to finalize increase memory pressure, since they stick around longer and require a deeper GC to collect.  Obviously this is not really good for performance.

Dispose comes in to the rescue by providing a common way for an object to be deterministically finalized, in the .NET sense (the managed memory is still only freed after the GC).  In a class implementation, Dispose does the real work of cleaning up resources, while Finalize simply calls Dispose. 

However, Dispose can also call the Dispose method of other IDisposable classes.  It's possible that a class that has no direct unmanaged resources might actually be using some unmanaged resources.  Thus, it's important that you call Dispose on every object that supports it, since you never know what the implementation might be.

How big a deal is this?  I wrote a small benchmark that creates a new finalizable object, does some work, and then either lets it fall out of scope, or calls dispose.  It repeats this 10,000,000 times.  The work done is generating a new random number (using a System.Random instance, which is created before the loop).  The finalizer calls dispose, which does nothing (just trying to get the overhead of finalization).

My test machine is a Pentium 4c (HyperThreaded) at 3GHz (533MHz FSB), with 1.5GB of syncronous DDR333 RAM.  When not calling dispose, the average run time is 6.48 seconds.  When calling dispose, the average run time is 3.17.  Calling dispose makes this over twice as fast.  View the code: FinalizeBenchmark.cs.txt (2.08 KB).

Bottom line: Always call dispose if you can, even if the class doesn't have unmanaged resources or a finalizer.

Code
Thursday, February 05, 2004 10:25:20 PM UTC  #    Comments [1]  |  Trackback Tracked by:
"http://aajs1yy.biz/spanking-wife.html" (http://aajs1yy.biz/spanking-wife.html) [Pingback]
"http://qo1oirm.biz/hairy-mexican-pussy.html" (http://qo1oirm.biz/hairy-mexican-... [Pingback]
"http://coppohq.biz/atl-the-movie.html" (http://coppohq.biz/atl-the-movie.html) [Pingback]


Sunday, August 22, 2004 2:55:54 AM UTC
Nice benchmark Michael. Your point about finalization is dead on. By not calling dispose you're forcing your objects to go through finalization.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview