Microsoft KB Archive/164494

= PRB: COM Objects Created in JScript Not Released Immediately =

Article ID: 164494

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Visual Basic, Scripting Edition 2.0
 * Microsoft Visual Basic, Scripting Edition 3.0
 * Microsoft Visual Basic, Scripting Edition 4.0
 * Microsoft Visual Basic, Scripting Edition 5.0
 * Microsoft JScript 2.0
 * Microsoft JScript 3.0
 * Microsoft JScript 4.0
 * Microsoft JScript 5.0

-



This article was previously published under Q164494



SYMPTOMS
COM objects that were created in JScript may not be released immediately after their last reference in script is out of scope or set to null.



CAUSE
The Microsoft JScript engine uses deferred garbage collection to free unused objects. The garbage collector improves script engine performance by delaying object destruction until a more economical time.



RESOLUTION
If you develop objects that will be used in script, you should consider this behavior of the garbage collector when you design the objects. In those extreme situations when objects must have their resources cleaned on demand, Microsoft recommends that you add methods to the object to provide for this.

For example, an object that loads a large text file into memory could provide a CloseFile method to script to allow for the immediate release of the memory buffers that are used by the object. In this case, although the object is still held in memory until the garbage collector is ready to release it, the huge effect of the text file buffer is alleviated.



MORE INFORMATION
Visual Basic (VBScript) currently does not use garbage collection.

Steps to Reproduce the Behavior
The following script demonstrates the problem. When you set the object reference to null, the object is marked as available for garbage collection, but it is not released until the garbage collector runs or the script engine is destroyed: var objTest;

objTest = new ActiveXObject("MyTestObject.TestObjects"); objTest.DoStuff; objTest = null; NOTE: You can use the undocumented CollectGarbage method to force garbage collection. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

266088 BUG: Excel Does Not Shut Down After Calling the Quit Method When Automating from JScript

