Solved

Using Close, Dispose and Nothing

Posted on 2008-06-12
4
615 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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