Detailed Explanation of JavaScript CollectGarbage Function Case

  • 2021-11-13 06:24:31
  • OfStack

First, look at an instance of memory release:

strTest = "1";
for ( var i = 0; i < 25; i ++ )
 strTest += strTest;
delete strTest;

CollectGarbage is a unique attribute of IE, which is used to release memory. The usage method should be to set the variable or reference object to null or delete, and then release it. Before doing CollectGarbage, two necessary conditions must be clear:

An object is invalidated if it is outside the context in which it exists.
-1 global object will be invalidated if it is not held (referenced).

// JavaScript When does the object fail 

function testObject() {
var _obj1 = new Object();

function testObject2() {
var _obj2 = new Object();
return _obj2;

//  Example 1

//  Example 2

//  Example 3
var obj3 = testObject2();
obj3 = null;

//  Example 4
var obj4 = testObject2();
var arr = [obj4];
obj3 = null;
arr = [];

In these four examples:

-"Example 1" constructs _ obj1 in the function testObject (), but when the function exits,
It has left the context of the function, so _ obj1 is invalid;

-In "Example 2", an object _ obj2 is also constructed and outgoing in testObject2 () because
This object has an "out-of-function" context (and life cycle), however, due to the function
The return value of is not "held" by other variables, so _ obj2 is also immediately invalid;

-In "Example 3", the _ obj2 constructed by testObject2 () is held by the external variable obj3,
Until the line "obj3=null" takes effect, _ obj2 will not be used because of the reference relationship
Disappear and fail.

-For the same reason as example 3, _ obj2 in "example 4" appears in the line "arr= []"
It will not expire until later.

However, the "invalidation" of an object does not wait for "release". Within the JavaScript runtime environment, there is no
There is any way to tell the user exactly "when the object will be released". This depends on JavaScript
The memory reclamation mechanism of. -This strategy is similar to the recycling mechanism in. NET.

In the previous Excel operation sample code, the owner of the object, that is, the process "EXCEL. EXE"
It can only occur after "ActiveX Object instance release". And the lock of the file, and the operation
The permissions credentials of the system are process-related. Therefore, if the object is only "invalid" instead of "released",
Then other processes will have problems when processing files and referencing permission credentials of the operating system.

-Some people say this is BUG of JavaScript or COM mechanism. Actually, it's not. It's OS, IE
And JavaScript are caused by a complex relationship, not an independent problem.

Microsoft discloses a strategy to solve this problem: actively invoke the memory reclamation process.

An CollectGarbage () process (commonly referred to as GC process) is provided in (Microsoft's) JScript.
The GC procedure is used to clean up "failed object misexamples" in the current IE, that is, the destructor process of calling objects.

The code to call the GC procedure in the above example is:

//  Deal with ActiveX Object When, GC Standard way to call procedures 

function writeXLS() {
//( Slightly ...)

excel = null;
setTimeout(CollectGarbage, 1);

Line 1 calls the excel. Quit () method to cause the excel process to abort and exit, when the JavaScript
The environment holds an excel object instance, so the excel process does not actually abort.

Line 2 makes excel null to clear the object reference and "invalidate" the object. However, due to
The object is still in the function context, so if you call the GC procedure directly, the object will still not be cleaned.

Line 3 uses setTimeout () to call the CollectGarbage function with an interval of '1', only
Is to cause the GC procedure to occur after the writeXLS () function is executed. Thus, the excel object satisfies the requirement of "being able to be
GC cleanup "Two conditions: no reference and out of context.

The use of the GC process works well in an JS environment where ActiveX Object is used. 1 Some potential ActiveX
Object includes XML, VML, OWC (Office Web Componet), flash, and even VBArray included in JS.
From this point of view, ajax architecture adopts XMLHTTP, and at the same time, it should meet the requirements of "no page switching"
Feature, so actively calling the GC process at an appropriate time will get better efficiency with UI experience.

In fact, even with the GC process, the aforementioned excel problem will not be completely solved. Because IE also
Permission credentials are cached. The only way to get the permission credentials of a page updated is to switch to a new page.
So in fact, in the SPS project mentioned earlier, my approach is not GC, but the following 1
Segment code:

//  Deal with ActiveX Object The page switching code used when 

function writeXLS() {
//( Slightly ...)

excel = null;

//  The following code is used to resolve the IE call Excel Adj. 1 A BUG, MSDN Methods provided in :
// setTimeout(CollectGarbage, 1);
//  Because it cannot be cleared ( Or synchronization ) Trusted Status of Web Pages ,  So it will lead to SaveAs() And other methods in 
//  Invalid on next call .

Explanation of delete operator in manual


Delete 1 attribute from an object, or 1 element from an array.

delete expression

The expression parameter is a valid JScript expression, typically a property name or an array element.


If the result of expression is 1 object and the attribute specified in expression exists and the object does not allow it to be deleted, false is returned.

In all other cases, true is returned.

Finally, a supplementary note about GC: When the IE form is minimized, IE will actively call the CollectGarbage () function once. This makes the memory footprint significantly improved after the IE window is minimized.

Related articles: