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

Jerry Lees Jerry Lees Profile: Jerry Lees

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

3  Comments on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • TonyLongson
    Eric Lippert (architect of VBScript) had this to say on setting objects to Nothing in VBScript: [A href=""]
    0 pointsBadges:
  • TonyLongson
    Oops, the link didn't appear:
    0 pointsBadges:
  • Jerry Lees
    Tony, Excellent [A href=""]article on vbscript's memory management[/A] 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!
    5,335 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: