By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Nothing or Dispose?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a

"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

P: n/a
> 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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.