Question Disposing an instance of a class

Brianb13

New member
Joined
May 25, 2008
Messages
1
Programming Experience
1-3
If I have a class that instances a class initially and calls that class several times using the single instance, then the called class will persist in memory which is what I want. How can I explicitly dispose of this instance? I heard that I could set the instance to null but I'm not sure how to do that yet. Thanks.
 
You can't set an instance to null. An instance is an instance and it will always be that instance. You can set a variable to null, or actually Nothing in VB, but that doesn't dispose an object.

If the object has a Dispose method then you need to call that method in order to dispose the object. If the object has no Dispose method then you can't dispose it because it doesn't require disposal.

Disposing an object will release any managed or unmanaged resources that that object is holding. That is very important in allowing the system to manage itself. That said, the memory an object occupies is NOT a resource in that sense. Even after disposal an object still exists in memory. If the variable that refers to that object loses scope soon after then you don't need to worry about it any further. If the variable does not, or may not, lose scope for some time then you should set it to Nothing. By setting the variable to Nothing you are removing that reference from the system and therefore making that object available for garbage collection. Garbage collection is the act of the system reclaiming the memory the object physically occupies.
 
Back
Top