Clean up your toys when your done: Fighting memory leaks in your scripts - The VBScript Network and Systems Administrator's Cafe

The VBScript Network and Systems Administrator's Cafe

Nov 5 2008   4:10PM GMT

Clean up your toys when your done: Fighting memory leaks in your scripts



Posted by: Jerry Lees
tips and tricks, VBScript, working with objects, memory leaks

One on the things you always want to do when you create objects in your scripts, especially objects from third party companies is, is to always remember to destroy the objects when you are done with them. At the very least, before you exit the script.

Object creation and not destroy the objects can be one source of memory leaks. In order to destroy an object should use the object’s .dispose method. If it does not have a dispose method, you set simply it to a special value of nothing. This will destroy the object and free up it’s memory. An example of using nothing is shown below:

Dim ObjTest
Set ObjTest = GetObject(”winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2″)
ObjTest = nothing

Comment on this Post


You must be logged-in to post a comment. Log-in/Register

TonyLongson  |   Dec 12 2008   10:27AM GMT

Eric Lippert (architect of VBScript) had this to say on setting objects to Nothing in VBScript:


 

TonyLongson  |   Dec 12 2008   10:29AM GMT

Oops, the link didn’t appear:

 <a href="http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx" title="http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx" target="_blank">http://blogs.msdn.com/ericlippert/archiv…</a>


 

Jlees  |   Dec 12 2008   3:28PM GMT

Tony,

Excellent article on vbscript’s memory management it expands on the topic and even claims that vbscript will automatically clear up itself when the object goes out of scope… but goes on to say if it’s not broken why fix it.

Thanks for adding to the discussion… keep ‘em coming!