• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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

0
Sanmarie
Asked:
Sanmarie
  • 2
  • 2
1 Solution
 
Jaime OlivaresSoftware 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.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now