473,395 Members | 1,484 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,395 software developers and data experts.

Dispose Overkill?

I exhaustively dispose/close anything database related and have managed to
control connection leaks.

However, I was wondering about the necessity to use Dispose on non-database
resources.

1. Take this code:
Sub X ()
Dim oItem as DataGridItem
For Each oItem In ctlMembers.Items
Call DoSomething(oItem)
Next
End Sub

The DataGridItem supports a Dispose method. Should I be using it?
Also, should I be setting oItem = Nothing

2. I have an object that was written by a 3rd party that didn't seem to
cause memory leaks when used in VB6. It doesn't have an explicit cleanup
method like Dispose. Do you think it is still getting cleaned up when used
in .Net.

Thanks =)
Jul 21 '05 #1
7 1606
Shawn Brock <Sh********@discussions.microsoft.com> wrote:
I exhaustively dispose/close anything database related and have managed to
control connection leaks.

However, I was wondering about the necessity to use Dispose on non-database
resources.

1. Take this code:
Sub X ()
Dim oItem as DataGridItem
For Each oItem In ctlMembers.Items
Call DoSomething(oItem)
Next
End Sub

The DataGridItem supports a Dispose method. Should I be using it?
Only if you "own" the DataGridItem, which in this case it doesn't sound
like you do. (It also only makes sense when you've definitely finished
with the item, which again doesn't particularly look like it's the case
here.)
Also, should I be setting oItem = Nothing
No, that would have no effect.
2. I have an object that was written by a 3rd party that didn't seem to
cause memory leaks when used in VB6. It doesn't have an explicit cleanup
method like Dispose. Do you think it is still getting cleaned up when used
in .Net.


I wouldn't like to say, to be honest.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Shawn,

Almost every object used in Net has the dispose method. (Not all).

It derives from system.ComponentModel.Component
http://msdn.microsoft.com/library/de...classtopic.asp

This classes derives from it and of course all classes that derives from
those.
http://msdn.microsoft.com/library/de...shierarchy.asp

Therefore when dispose is not overloaded as with the connection, than you
should in my opinion have to ask yourself why you do it. The result of
calling it, does often not more than the sentence a += 0; Although it does
not harm of course when you place this even a million times in your program.

The benefit can be, that when the GC starts working and if it is about a
kind of globaly placed object, it is possible that it needs less time to
release the memory for that.
(If there is a reference to or from an object the GC will never release it,
with or without dispose)

I hope this gives an idea.

Cor
Jul 21 '05 #3
Thank you folks for your quick and insightful response.
I went from being an MCSD in VB to feeling like a complete Novice in .Net.
Things are starting to come together, but I have some ASP.NET work that is
dying strange deaths.

Thanks again!
"Cor Ligthert" wrote:
Shawn,

Almost every object used in Net has the dispose method. (Not all).

It derives from system.ComponentModel.Component
http://msdn.microsoft.com/library/de...classtopic.asp

This classes derives from it and of course all classes that derives from
those.
http://msdn.microsoft.com/library/de...shierarchy.asp

Therefore when dispose is not overloaded as with the connection, than you
should in my opinion have to ask yourself why you do it. The result of
calling it, does often not more than the sentence a += 0; Although it does
not harm of course when you place this even a million times in your program.

The benefit can be, that when the GC starts working and if it is about a
kind of globaly placed object, it is possible that it needs less time to
release the memory for that.
(If there is a reference to or from an object the GC will never release it,
with or without dispose)

I hope this gives an idea.

Cor

Jul 21 '05 #4
Cor Ligthert <no************@planet.nl> wrote:
Almost every object used in Net has the dispose method. (Not all).


I think that's a bit of an overstatement.

I wrote a quick program to find all the public types in selected
assemblies, and see how many support IDisposable. Here are the results:

mscorlib: 60/903
System.Configuration.Install: 6/14
System.Data: 29/181
System.Data.OracleClient: 8/28
System.Design: 43/102
System.DirectoryServices: 3/22
System: 35/475
System.Drawing: 28/168
System.EnterpriseServices: 3/104
System.Management: 8/67
System.Messaging: 7/46
System.Runtime.Remoting: 2/26
System.Security: 0/21
System.ServiceProcess: 5/18
System.Web: 79/321
System.Web.Services: 8/143
System.Windows.Forms: 79/331

Overall: 403/2970

If you think that's an unrepresentative set of assemblies, let me know
which ones should be considered instead. (Of course, it's possible that
my program has a bug - let me know if you want me to post it.)

A lot of the types used in GUIs, for IO, and for database access
implement IDisposable - but there's a lot more to the framework than
that. (Also, you usually don't need to explicitly dispose of GUI
objects as everything tends to be disposed when the top-level form gets
disposed.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
Jon,
I wrote a quick program to find all the public types in selected
assemblies, and see how many support IDisposable. Here are the results:


I will change my writing about this, I was struggling a little bit with the
text when I wrote this.

I was thinking about something as, "the normally most often used classes in
Net", this will probably make my intention with this writing better and I
shall use that next time.

Thanks for making me attend on that and giving me that figure "more than
15%", I will probably use that as percentage than as well to make it more
clear.

Cor
Jul 21 '05 #6
Cor Ligthert <no************@planet.nl> wrote:
I wrote a quick program to find all the public types in selected
assemblies, and see how many support IDisposable. Here are the results:
I will change my writing about this, I was struggling a little bit with the
text when I wrote this.

I was thinking about something as, "the normally most often used classes in
Net", this will probably make my intention with this writing better and I
shall use that next time.


But even then I'd disagree with you. It entirely depends on what you're
doing - in my day-to-day work I use *far* more non-disposable types
than disposable types.
Thanks for making me attend on that and giving me that figure "more than
15%", I will probably use that as percentage than as well to make it more
clear.


I'm not sure it *does* make it clearer, but it's up to you, of course.

I'm also not sure where overloading comes into whether or not you
should call Dispose.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
Jon,

I'm also not sure where overloading comes into whether or not you
should call Dispose.


I think that than you have not to ask yourself why you do it, as I wrote,
but than you can read it.

:-)

Cor
Jul 21 '05 #8

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

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
7
by: Shawn Brock | last post by:
I exhaustively dispose/close anything database related and have managed to control connection leaks. However, I was wondering about the necessity to use Dispose on non-database resources. 1. ...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.