Re: About speed



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/
.



Relevant Pages

  • Re: " //Clean Up managed resources " f&*ck
    ... The finalizer is called by the GC. ... time (such as releasing internal unmanaged resources). ... Since Dispose ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dipose vs Destructor
    ... resources and native resources in a managed C++ class? ... C++/CLI implements the destructor and finalizer semantics in any ref class T ... These cleanup mechanisms are hidden from the C++/CLI ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Finalizer vs. Dispose
    ... You can read more on Dispose guidelines here: ... |> Mostly all .NET classes which implements IDisposable, ... The Finalizer is really there for non-managed resources that ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Finalizer vs. Dispose
    ... > Mostly all .NET classes which implements IDisposable, ... > an finalizer. ... If you don't have those sorts of resources, ... If all you hold are managed resources, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: GC and dispose
    ... resources as well as unmanaged. ... We tend to use the Finalizer ... The Dispose method should be used to free resources. ... protected virtual void Dispose ...
    (microsoft.public.dotnet.framework)