Using Close, Dispose and Nothing

Using .NET 2.0, VS 2005, VB.net

I have a connection object (global scope) and other objects that I want to remove from memory when finished. I call Close, then Dispose then set it to nothing. Is this a good way to clear up the object or is this too much. Also, I call the garbage collector after all of this.

Thanks
San


myOleDbConnection.Close()
myOleDbConnection.Dispose()
myOleDbConnection = Nothing
 
GC.Collect()

Open in new window

SanmarieAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
Close() will be enough, Dispose() will call close anyway.
If you plan to use the connection variable again, then use Nothing, if not, then it is not needed.
GC.Collect() will not collect when you want, the .net environment will decide the proper time.
0
 
SanmarieAuthor Commented:
Thanks Jaime_Olivares,

Then what's the purpose of anyone calling GC.Collect() if the garbage collector is going to run on its own time?

San
0
 
Jaime OlivaresSoftware ArchitectCommented:
GC.Collect() is not commonly used, there are several discussions about that in Intenet you can google.
0
 
SanmarieAuthor Commented:
Ok. Fair enough. Thanks

San
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.