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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.Dispose versus Set = Nothing 1

Status
Not open for further replies.

pzmgmb

Programmer
Apr 2, 2003
115
0
0
US
At the risk of getting my wrists slapped by the Tek-Tips community, I'm going to ask this question...

I've searched the forums and cant seem to find a post or a FAQ that clears it up for me.

If this is my piece of code in VB .NET...

Code:
dim frm as new Form1
frm.datagrid1.datasource= me.dataset1
frm.show

How do I release the memory for this after I'm done with it?

Do I use the Dispose method? Or do I set frm = nothing?

What do i do if the objects i'm using dont support the IDisposable interface?

Thanks
 
hi,

i think you can do frm = nothing, the garbage collector should free some memory because nothing is pointing at the resources used by frm anymore.
 
.Dispose and = Nothing will acheive the same thing in the end. .Dispose will fire the Dispose event which can by caught and handled, but after that the memory reference will be unlocked (Meaning the memory is still there, but it can be overwriten). Memory handling is completely controled by the Garbage Collector. Some times it may seem like there is stuff that's just not being collected, and most of the time it's just because the Garbage collected has decided the process of clearing that memory is not worth the time it would take.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
If you feel you need to force a garbage collection then you can use:

GC.Collect

but you should only need this in rare circumstances (an example might be where you're created and destroying multiple memory intensive items such as images very frequently)
 
Agree with jubble -- if your code is clean, you will likely never need to call GC.Collect. The .net memory collection algorithm is very good, and is essentially the same as in Java.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top