Nothing or Dispose?

N

N! Xau

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
 
O

Oenone

N! Xau said:
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,
 
C

Cor Ligthert

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
 
B

Brian Gideon

To clarify, Dispose is not absolutely necessary, but is recommended.

Brian
 
M

m.posseth

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]
 
C

Cor Ligthert

"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
 
J

Jonathan Allen

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.
 
B

Brian Gideon

Cor said:
"Brian Gideon"


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.
 

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