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
Tags:
memory leaks
tips and tricks
VBScript
working with objects

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TonyLongson
    Eric Lippert (architect of VBScript) had this to say on setting objects to Nothing in VBScript: [A href="http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx"]
    0 pointsBadges:
    report
  • TonyLongson
    Oops, the link didn't appear: http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx
    0 pointsBadges:
    report
  • Jerry Lees
    Tony, Excellent [A href="http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx"]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:
    report

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: