In both JScript and VBScript, the object referenced by an object variable will be destroyed when the object goes out of scope if it is never explicitly set to nothing (VBScript) or null (JScript). The difference is what happens when you do set the object to nothing/null. In VBScript, that immediately destroys the object. In JScript, the object is destroyed when the garbage collector runs, which could be any time ( or forever if the free memory never drops).The JScript garbage collector works like this: 1) When the script engine is shut down, garbage is collected. 2) When 256 variants are allocated, or when more than 64K is allocated, then the garbage collector sets a flag that says "collect soon". 3) Whenever a new statement is executed OR we break into the script debugger, we check that flag, and if it is set, we do a collection. For TESTING purposes, there is a "collectGarbage" function which forces a collection. I don't recommend shipping code that uses this function -- you should design your programs to work well with automatic memory management. If you have an algorithm which _depends_ on having a predictable collection sequence, JScript is not an appropriate language for you to solve your problem. Use a language with predictable GC behaviour, like VBScript.
will be destroyed when the object goes out of scope if it is never
explicitly set to nothing (VBScript) or null (JScript). The difference is what happens when you do set the object to nothing/null.
In VBScript, that immediately destroys the object. In JScript, the object
is destroyed when the garbage collector runs, which could be any time ( or forever if the free memory never drops).The JScript garbage collector works like this:
1) When the script engine is shut down, garbage is collected.
2) When 256 variants are allocated, or when more than 64K is allocated,
then the garbage collector sets a flag that says "collect soon".
3) Whenever a new statement is executed OR we break into the script
debugger, we check that flag, and if it is set, we do a collection. For TESTING purposes, there is a "collectGarbage" function which forces a
collection. I don't recommend shipping code that uses this function -- you
should design your programs to work well with automatic memory management.
If you have an algorithm which _depends_ on having a predictable collection
sequence, JScript is not an appropriate language for you to solve your
problem. Use a language with predictable GC behaviour, like VBScript.