Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nothing & Dispose

Status
Not open for further replies.

ProjectExplorer

Programmer
Mar 22, 2002
95
GB
What is the general rule on setting objects to Nothing & Dispose in VB.Net.

I read some where the VB.NET "cleans up" and releases onjects. When reading about VB.Net some examples set objects like OleCommand objects to Nothing and Dispose of them whilst others don't....

What is the general rule?

Thanks.
 
Let VB.Net handle "cleanup" which is referred to as Garbage Collection (GC). If you code is using COM objects, or is showing a general memory leak, then you have a couple options.

One is to track your code and identify what variables are not being automatically GC'ed, and either rewrite the code, or attempt to force a garbage collection.

I had some code that was running a little memory intensive, when it shouldn't be. I added some forced GC's for my vb6 COM objects and it cleared it up, a little.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Thanks for clarifying this Qik3Coder - also explains why "Nothing" and "Dispose" exist when we have the Garbage Collection.
 
Nothing exists for a different reason than GC. Nothing is the state that most variables are in when they are Dim'ed but not set. I have a VB class I just wrapped up, and the best example for what a variable is, that I could come up with, is a garbage can.

You create the garbage can (DIM), you name it (intWhatever), and later on you put something in it (Assignments). Until you put something in it when you "look in" it has (NOTHING) in it.

The dispose is the opposite of a New(), it's where you do the clean up, close DB conx, nothing-ize your variables, so the GC knows it can destroy them.


If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Thanks for explaining this further -

Only thing I don't quite grasp here is how does the GC know whether a NOTHING variable (effectively memory set aside but not used - i.e. just DIM'ed) isn't just waiting for a value. At what point are they ready to be "Garbaged"?
 
it is gc'ed when nothing can have access to it. for example when you create a local variable in a local _Click event. Once the End Sub is hit, it knows nothing else can touch it. So it GC's it, whether it has been nothing'ed or not.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
My understanding is scope determines that. If you DIM a variable in a function. It will only exist inside that function. Therefore the GC will dump it when you exit the function (at least that's the concept). Class level functions only exist while the class is loaded and Public Module variables persist for the entire duration of the application to sight a few examples.

Now the GC doesn't always run on time. It has been known to be "late" for disposal. What this means is the memory is still allocated, but not available for use. Like setting your trash out to the curb, it's going, but just hasn't quite made it to the truck yet.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Many thanks for all your explanations. I'll will keep it for reference - promise not to stick it in the garbage!

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top