I am wondering what would be the most correct way to deal with memory when using VBScript. Should declare all variables right before I use them? The beginning of the program? I understand global vs local, however in my script all variables are local. I know that memory leaks will never be a problem when writing in VBScript 99.9% of the time, but I am also curious as to the 'best' way to clear and release memory within a script. By 'best' I mean, the timing of clearing variables/objects (right after you are done using them vs the end of the script), etc.
An example:
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim arrList : Set arrList = CreateObject("System.Collections.ArrayList")
Dim objDict: Set objDic = CreateObject( "Scripting.Dictionary")
Dim objEmail : Set objEmail = CreateObject("CDO.Message")
Dim someArray(), x, y, z, item
The VBScript garbage collector runs at the end of every line to clear implicit variables and at the end of every procedure (
end sub
,end function
, andend property
) to clear explicit variables. Objects are similar but have added constraints. It works similar to VBA's garbage collector. By contrast JScript waits until 30,000 objects have gone out of scope before running and freeing memory.An implicit variable is an unnamed variable -
msgbox LCase(UCase("String")
has two implicit variables - the result ofUCase("String")
and that is passed toLCase(implicitVar1)
which returnsimplicitVar2
which is passed toMsgbox
. An Explict variable is declared either byDIM
or just by using it as inA=5
which creates an explicit variable calledA
.https://blogs.msdn.microsoft.com/ericlippert/2003/09/17/how-do-the-script-garbage-collectors-work/
https://blogs.msdn.microsoft.com/ericlippert/2004/12/22/t4-vbscript-and-the-terminator/
Also
https://blogs.msdn.microsoft.com/ericlippert/2004/04/28/when-are-you-required-to-set-objects-to-nothing/
https://blogs.msdn.microsoft.com/ericlippert/2004/03/01/syntax-semantics-micronesian-cults-and-novice-programmers/
The CPU is a stack based machine (and VBScript a stack based virtual machine). When the CPU calls a function the calling program puts the parameters on the stack and the return address, adjust the stack frame and does a jump. The callee function creates local variables on the stack and also places the return value on it. When it returns the stack pointer is adjusted back to where it was which automatically frees all the above.
It's best practice to declare all variables, but not for the reason you assume. VBScript is sufficiently good at cleaning up after itself, so memory leaks usually aren't an issue. Most of the time you don't even need to release objects (
Set var = Nothing
) because they're automatically destroyed when leaving the context.The reason why you still want to declare your variables is that you want to use
Option Explicit
in your scripts (which enforces variable declarations), so that you can avoid problems due to mistyped or otherwise uninitialized variables. WithoutOption Explicit
VBScript would automagically create missing variables and initialize them with an empty/zero value. Silly example:Running the above would create an infinite loop. If you add
Option Explicit
the script will instead immediately terminate with a runtime error: