473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Garbage Collector and dispose

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
Jun 21 '07 #1
11 1867

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
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/lib...e.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>

Jun 21 '07 #2
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

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
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

Jun 21 '07 #3
thanks for this hint!

Regards
Rainer
Jun 21 '07 #4
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

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
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

Jun 21 '07 #5
On Jun 21, 2:19 pm, "PvdG42" <p...@toadstool.eduwrote:
Read the remarks section here:

http://msdn2.microsoft.com/en-us/lib...e.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...3-20c06ae539ae

Jon

Jun 21 '07 #6
On Jun 21, 2:21 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
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

Jun 21 '07 #7
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.

"Kevin Spencer" wrote:
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

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
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


Jun 21 '07 #8
On Jun 21, 2:49 pm, burlo <b...@discussions.microsoft.comwrote:
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

Jun 21 '07 #9
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
Jun 21 '07 #10
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.

Jun 22 '07 #11
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[MSFT]" <st*****@online.microsoft.comschrieb im Newsbeitrag
news:uw**************@TK2MSFTNGHUB02.phx.gbl...
Hope this also helps.
That is for sure ;-)

Regards
Rainer
Jun 22 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
18
by: Rein Petersen | last post by:
Is there any way to adjust thread priority for the garbage collector? Would be nice if I could tune the thread priority rules for the garbage collector too... From my readings of the process,...
10
by: Ray5531 | last post by:
Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself?? Is there away to to do so? Thanks
36
by: André | last post by:
Hello, I have a c# application that creates a timer to fire every minute using multithreading. On every minute it calls a Ping class that I made using sockets. It creates a new ping class then...
5
by: Ben | last post by:
Could someone please verify if what I am doing as follow is corrected: 1. when dealing with custom class objects: ..... public myObject as myClass myObject as New myClass .......here I am...
14
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our...
4
by: PromisedOyster | last post by:
There are various contradictory newsgroup postings on this issue, but I would really like a definitive answer from the .NET gurus out there? We have various WinForms that contain multiple Icons...
3
by: cbmeeks | last post by:
I think I know the answer to this but I would like to confim. I have an app where I've built a custom DataSet containing several DataTables. Within the DataTables, I sometimes check for...
8
by: Paul.Lee.1971 | last post by:
Hi everyone, A program that I'm helping to code seems to slow down drastically during initialisation, and looking at the profiling graph, it seems to be the garbage collector thats slowing things...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.