Garbage Collector and dispose

R

Rainer Queck

Hi NG,

just a short question: If a class implements the IDisposable interface, does
the garbage collector always call the Dispose() method of the class, before
it is cleaned up?

Regards
Rainer
 
P

PvdG42

Rainer Queck said:
Hi NG,

just a short question: If a class implements the IDisposable interface,
does the garbage collector always call the Dispose() method of the class,
before it is cleaned up?

Regards
Rainer

Read the remarks section here:

http://msdn2.microsoft.com/en-us/library/system.idisposable.dispose.aspx

<quote>
Because the Dispose method must be called explicitly, objects that implement
IDisposable must also implement a finalizer to handle freeing resources when
Dispose is not called.
</quote>
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You should call it, either by direcly calling it or by using the using
construction:

using(SqlConnection con = new SqlConnection ...)
{
} //Disposed here


But answering your original question, the answer is yes, it does call it
when finalizing the object (which is not deterministic)

Take a look at the "Implementing a Dispose method" in MSDN

Also note that you only need to implement it when you are using non-managed
resources
 
K

Kevin Spencer

It does, if the Finalize method of the class calls the Dispose method. While
Microsoft classes all do this when necessary, you might want to be wary of
3rd-party classes, which may or may not.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
J

Jon Skeet [C# MVP]

Read the remarks section here:

http://msdn2.microsoft.com/en-us/library/system.idisposable.dispose.aspx

<quote>
Because the Dispose method must be called explicitly, objects that implement
IDisposable must also implement a finalizer to handle freeing resources when
Dispose is not called.
</quote>

That's a terrible piece of advice, however. Most types which implement
IDisposable *don't* need to implement a finalizer, because usually
they'll just be calling Dispose on something else. For instance,
StreamReader doesn't need a finalizer - it relies on any stream which
*directly* holds unmanaged resources to have a finalizer instead.

Much better advice is given in Joe Duffy's blog:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

Jon
 
J

Jon Skeet [C# MVP]

You should call it, either by direcly calling it or by using the using
construction:

using(SqlConnection con = new SqlConnection ...)
{

} //Disposed here

But answering your original question, the answer is yes, it does call it
when finalizing the object (which is not deterministic)

Only if you implement a finalizer which calls Dispose manually. The GC
doesn't automatically call Dispose itself - it just calls the
finalizer (if there is one) which can be told to call Dispose.

Jon
 
G

Guest

Its always best to call Dispose explicitly as Finalize can have a negative
impact on performance. When Dispose is called the Object's Finalize method
will not be called.
 
J

Jon Skeet [C# MVP]

Its always best to call Dispose explicitly as Finalize can have a negative
impact on performance. When Dispose is called the Object's Finalize method
will not be called.

Again, this depends on a call to GC.SuppressFinalize within Dispose.

Basically, the CLR itself does *nothing* with Dispose. Any connections
you want between Dispose and finalization have to be put there
explicitly in code (in C#, anyway).

Jon
 
R

Rainer Queck

Hi Ignacio,
Also note that you only need to implement it when you are using
non-managed resources
Thanks for this hint. I actually don't need it then, since all code is
currently managed.

Thanks
Rainer
 
S

Steven Cheng[MSFT]

Hi Rainer,

As other members have mentioned, generally for those classes that has
implemented the IDisposable interface, that means it will hold unmanaged
resources. And the class deisgner will provide the following means to let
those unmanaged resource be proper released:

** Let the user explicitlly call Dispose method to release the resource on
demand. This is most recommend since it will help make the resource be
released as earlier as possible.

** If the user doesn't call Dispose method, the Finalizer method of the
class(class designer should also put resource release code in it) will be
called when class instance be Garbage collected

So for your question, if the class is a well encapsulated one, you can
think that at GC time, the finalizer will help release those resource(just
like you call Dispose method). But manually call Dispose(if you know that
the resource is no longer needed) is always preferred.

BTW, if you have interests, you can use Reflector tool to inspect those
classes in .NET framework library which have implemented IDisposable
interface to get some further idea on this.

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rainer Queck

Hello Steven,

thanks for putting the facts together!
I think I now have a very good vision of what to do, if I should need to
handle a unmanaged resouce.

Steven Cheng said:
Hope this also helps.
That is for sure ;-)

Regards
Rainer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top