473,394 Members | 2,090 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,394 software developers and data experts.

Nothing or Dispose?

Hello everybody,
I am kind of a newbie. What's the difference between using dispose() or
= nothing in the 'finally' block of the following code:

Dim mycmd As New OracleCommand(setta_cmdQuery, con)
' setta_cmdQuery is a simple UPDATE query
mycmd.CommandType = CommandType.Text
Try
mycmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'mycmd.Dispose()
mycmd = Nothing
End Try
THANKS
N! Xau
keep in mind the power of ANTANI
http://ilovemiliofede.altervista.org

Nov 21 '05 #1
7 11211
N! Xau wrote:
I am kind of a newbie. What's the difference between using dispose()
or = nothing in the 'finally' block of the following code:


Setting the object to Nothing will cause you to lose your reference to the
object, but nothing else will immediately happen (unlike in VB6, where if
this was the only reference then the object would have terminated).

Providing there are no other references to the object, it will be finalized
and garbage collected the next time the garbage collector runs. However, the
resources used by the object will NOT be released until the garbage
collection occurs (which could be some considerable time in the future).
This potentially continues to consume memory and other resources both on the
client (in ADO.NET) and the server (in your Oracle database).

If you Dispose of the object, you explicitly release all resources used by
the object. This will cause it to free all of its memory and properly
terminate itself.

You should therefore always Dispose of your objects as soon as you are
finished with them. You can set them to Nothing afterwards too if you like,
but setting things to Nothing doesn't in itself cause things to be tidied up
any more.

Hope that helps,

--

(O)enone
Nov 21 '05 #2
N,

They both are not needed in this situation. (They only cost some nanoseconds
and lines of code)

The procedure goes out of scope and the GC will clean it up in his Gen 0,
which runs normally very often.

The name is not for nothing managed code.

I hope this helps,

Cor
Nov 21 '05 #3
To clarify, Dispose is not absolutely necessary, but is recommended.

Brian

Cor Ligthert wrote:
N,

They both are not needed in this situation. (They only cost some nanoseconds
and lines of code)

The procedure goes out of scope and the GC will clean it up in his Gen 0,
which runs normally very often.

The name is not for nothing managed code.

I hope this helps,

Cor


Nov 21 '05 #4


Setting objects to Nothing is still a good coding practice ( and helps the
garbage collector ) however setting objects to Nothing for objects in a
method is a waste of the code line as there is no usage count for a object
that goes out of scope at the end of the method

Michel Posseth [MCP]
"Oenone" <oe****@nowhere.com> wrote in message
news:eQ*************@TK2MSFTNGP12.phx.gbl...
N! Xau wrote:
I am kind of a newbie. What's the difference between using dispose()
or = nothing in the 'finally' block of the following code:


Setting the object to Nothing will cause you to lose your reference to the
object, but nothing else will immediately happen (unlike in VB6, where if
this was the only reference then the object would have terminated).

Providing there are no other references to the object, it will be
finalized and garbage collected the next time the garbage collector runs.
However, the resources used by the object will NOT be released until the
garbage collection occurs (which could be some considerable time in the
future). This potentially continues to consume memory and other resources
both on the client (in ADO.NET) and the server (in your Oracle database).

If you Dispose of the object, you explicitly release all resources used by
the object. This will cause it to free all of its memory and properly
terminate itself.

You should therefore always Dispose of your objects as soon as you are
finished with them. You can set them to Nothing afterwards too if you
like, but setting things to Nothing doesn't in itself cause things to be
tidied up any more.

Hope that helps,

--

(O)enone

Nov 21 '05 #5

"Brian Gideon"
To clarify, Dispose is not absolutely necessary, but is recommended.


It should be used where it is needed. And in the case of the OP's problem is
it only spending some program time. There are some statements from people
who tell that it should be used when it is in a class, because they made it
not for nothing they tell. For me tells that only something from the writers
of those statements.

It is in 20% of all classes, while those 20% are probably 80% from the most
used classes.

Cor
Nov 21 '05 #6
> They both are not needed in this situation. (They only cost some
nanoseconds and lines of code)
Well, that depends on whether or not the OracleCommand keeps the connection
open. I've seen web apps work fine on desktops machines but crash on big
servers. Why? Because the servers have a ton of memory, so the GC never
runs, thus causing the database to run out of connections.

--
Jonathan Allen
"Cor Ligthert" <no************@planet.nl> wrote in message
news:Ok**************@TK2MSFTNGP09.phx.gbl... N,

They both are not needed in this situation. (They only cost some
nanoseconds and lines of code)

The procedure goes out of scope and the GC will clean it up in his Gen 0,
which runs normally very often.

The name is not for nothing managed code.

I hope this helps,

Cor

Nov 21 '05 #7


Cor Ligthert wrote:
"Brian Gideon"
To clarify, Dispose is not absolutely necessary, but is recommended.

It should be used where it is needed. And in the case of the OP's problem is
it only spending some program time.


Calling Dispose will take the object off the finalization queue. So
all you're doing is moving the cleanup responsibility from the GC to
the application. Extra execution time on an application thread is
preferrable to extra execution time on a GC thread because the GC must
suspend application threads while it runs.
There are some statements from people who tell that it should be used when
it is in a class, because they made it not for nothing they tell. For me
tells that only something from the writers of those statements.
Well, it is there for a reason so use it. Not using it could result in
bugs.
It is in 20% of all classes, while those 20% are probably 80% from the most
used classes.

Cor


Nov 21 '05 #8

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

Similar topics

5
by: DraguVaso | last post by:
Hi, I have made some classes, but when they are Finalized/Disposed or simply "MyClass = Nothing", I want to trigger this action, and ask to save unsaved changes. How should I do this? I'm...
2
by: Asha | last post by:
greetings, should i do this obj.dispose then obj = nothing both does the same thing right? whats the core difference?
4
by: David Schwartz | last post by:
Seems that setting an object = Nothing no longer does anything in .NET. Garbage collection destroys the object eventually. So, does it ever make sense to set an object = Nothing in .NET? Thanks.
5
by: Eric Newton | last post by:
Ok, heres the scenario, this is code that you'll see peppered in most of my classes that utilize any kind of object that has a Dispose method Dim conn as SqlConnection = MakeSqlConnection() Try...
3
by: Cylix | last post by:
Does anyone can explain the details between Connection.close Connection.dispose Connection = nothing If I just need to close the connection and release the memory, do I need to write all...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
34
by: =?Utf-8?B?SGFvIEppYW5n?= | last post by:
Hi, I see in many MS example code that looks like this Public Sub Foo() dim myCustomer as New Customer ... myCustomer.Dispose() myCustomer = Nothing End Sub
10
by: Alan Mailer | last post by:
I'm relatively new to VB.net. Is it good practice to 'destroy' created objects... or does VB.net take care of this for you in its 'garbage' collection. For example, in VB6 we used to have to do...
7
by: andreas | last post by:
Can someone tell me 1) Is there a difference between closing or disposing a reference data type 2) about nothing dim fr as form2 isnothing(fr) gives true
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
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: 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:
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.