PRB: COM Objects Created in JScript Not Released Immediately (164494)



The information in this article 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

REFERENCES

For more information about VBScript and JScript, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:10/24/2003
Keywords:kbprb kbProgramming kbScript KB164494