Solved

Using Close, Dispose and Nothing

Posted on 2008-06-12
4
622 Views
Last Modified: 2013-12-17
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
Comment
Question by:Sanmarie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 21773466
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
 

Author Comment

by:Sanmarie
ID: 21773843
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21773934
GC.Collect() is not commonly used, there are several discussions about that in Intenet you can google.
0
 

Author Comment

by:Sanmarie
ID: 21774077
Ok. Fair enough. Thanks

San
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question