Re: About speed
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Mon, 26 Jun 2006 09:48:28 +0100
Robert Giesecke <Spam@xxxxxxxxx> wrote:
Apparently, Barry has RE'd more BCL classes than me.
And when he says it is extremely rare, well, I've no problem at all
accepting that.
I should make myself clearer: I think it's extremely rare that your
average library or application developer should need to implement a
finalizer - I've only needed to implement two in a relatively large
project, for collecting unmanaged resources. I think the best advice
backs me up on this. I think this is probably the best, most
comprehensive coverage of the topic, by Joe Duffy, a PM on the CLR team:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae
An excerpt:
---8<---
þ Do implement a finalizer if your object is responsible for controlling
the lifetime of at least one resource which does not have its own
finalizer. Types like SafeHandle, for example, have their own finalizers
responsible for cleaning up resources. [...]
[...]
For the majority of types implementing the Dispose pattern, you will not
need to implement your own Finalize method. This example shows the
simple case, for example when using a SafeHandle to take care of the
implicit cleanup[...]
--->8---
***
Anyway, finalizers are not as rare as they should be in the .NET
libraries because Component implements a finalizer (but does nothing
with that code path). I think it does it to cover its ass with respect
to new component authors who might need to override Dispose(bool), and
might feel obliged to implement the pattern (probably incorrectly)
themselves. Components are typically more heavyweight than classes
anyway.
Here are the statistics, for all types (public and private) in the .NET
libraries with .NET 2.0:
Total number of types: 23399
Disposable types: 1744
Types with a finalizer: 1056
Unique Finalize implementations: 157
Most objects which are finalizable have implemented their finalizer from
a base class. 157 is the number of classes in total that have declared a
finalizer themselves.
This is the top 10 used implementations of the Finalize method (i.e
classes in C# that defined a destructor):
Using System.ComponentModel.Component: 518
Using System.ComponentModel.Design.ComponentDesigner: 164
Using System.Runtime.InteropServices.SafeHandle: 148
Using System.Windows.Forms.DataGridViewCell: 12
Using System.Windows.Forms.DataGridViewBand: 9
Using System.ComponentModel.MarshalByValueComponent: 8
Using System.Drawing.Brush: 6
Using System.Runtime.InteropServices.CriticalHandle: 6
Using System.IO.FileStream: 5
Using System.Windows.Forms.HtmlShim: 4
It can be seen that most implementations of Finalize() are inherited
from Component, ComponentDesigner or SafeHandle. Many classes implement
Finalize simply to get logging (e.g. TcpClient, but not UdpClient),
while others implement it for no apparent reason (e.g.
DataGridViewCell). ComponentDesigner's finalizer does nothing too, apart
from call Dispose(false) - but almost all code will only be in the "if
(disposing)" branch anyway, so normally it doesn't do anything.
A technique worth commenting on: FileStream's finalizer only tries to
call Dispose(false) if the handle (a SafeHandle) for the stream hasn't
been collected yet. It does this in order to flush any pending write
buffer data, in an effort to clean up after developers who haven't
flushed their files. This is a distinct task from resource management,
which strictly requires only that the file be closed.
-- Barry
--
http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: About speed
- From: Robert Giesecke
- Re: About speed
- References:
- Re: About speed
- From: Bryce K. Nielsen
- Re: About speed
- From: Craig Stuntz [TeamB]
- Re: About speed
- From: Bryce K. Nielsen
- Re: About speed
- From: Ingvar Nilsen
- Re: About speed
- From: Bryce K. Nielsen
- Re: About speed
- From: Ingvar Nilsen
- Re: About speed
- From: Bryce K. Nielsen
- Re: About speed
- From: Joanna Carter [TeamB]
- Re: About speed
- From: Ingvar Nilsen
- Re: About speed
- From: Robert Giesecke
- Re: About speed
- From: Barry Kelly
- Re: About speed
- From: Alex
- Re: About speed
- From: Robert Giesecke
- Re: About speed
- Prev by Date: Re: About speed
- Next by Date: Re: About speed
- Previous by thread: Re: About speed
- Next by thread: Re: About speed
- Index(es):
Relevant Pages
|
|