JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Getting Rid of Objects When You're Done with Them

When you're done with an object and want to get rid of it so it no longer takes up memory and other resources, what do you do? (This isn't necessarily such a problem when you have one such object, but imagine if you have an array of 20,000 of them.) The official way to get rid of objects is to assign them the keyword Nothing. That might look like this:

Dim notifierObject As New Notifier()
notifierObject.Display("Hello from Visual Basic!")
notifierObject = Nothing

This tags the object for garbage collection, which was discussed in the In Depth section of this chapter. When the program gets around to collecting the garbage, this object will be destroyed. Note, by the way, that you don't have to do this at all unless you explicitly want to get rid of an object; if an object goes out of scope, it is automatically targeted for garbage collection. The Visual Basic documentation goes on a great deal about how you can't determine when garbage collection happens-but you can. See the next topic.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor