Collectgarbage function-js cleans up garbage and releases memory

Source: Internet
Author: User

First, let's look at an instance with memory released:

  <  Script Language  =  "  Javascript  "  >  
<! --
Strtest = " 1 " ;
For ( VaR I = 0 ; I < 25 ; I ++ )
{
Strtest + = Strtest;
}
Alert (strtest );
Delete Strtest;
Collectgarbage ();
// -->
< / SCRIPT>

Collectgarbage is a special attribute of IE used to release the memory. It should be used to set the variable or reference object to null or delete, and then perform the release action, before you perform collectgarbage, you must understand the following two prerequisites:

-An object becomes invalid in addition to its living context.
-A Global object becomes invalid if it is not used (referenced.

  // ---------------------------------------------------------  
// When the JavaScript Object expires
// ---------------------------------------------------------


Function Testobject (){
VaR _ Obj1 = New Object ();
}

Function Testobject2 (){
VaR _ Obj2 = New Object ();
Return _ Obj2;
}

// Example 1
Testobject ();

// Example 2
Testobject2 ()

// 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 testobject () function, but when the function exits,
It has left the context of the function, SO _ obj1 is invalid;
-In "Example 2", testobject2 () also constructs an object _ obj2 and transmits it out, because
This object has an external context (and lifecycle). However
The returned value is not "held" by other variables, SO _ obj2 also becomes invalid immediately;
-In Example 3, _ obj2 constructed by testobject2 () is used by the external variable obj3,
At this time, until the line "obj3 = NULL"CodeWhen the value of _ obj2 is valid, it will be due to the reference relationship.
It disappears and becomes invalid.
-For the same reason as Example 3, _ obj2 in Example 4 will be in the "arr = []" line of code.
And then it becomes invalid.

However, the "invalid" of the object will not be "released ". In the Javascript runtime environment, no
There is any way to tell the user exactly when the object will be released ". This depends on JavaScript
Memory recovery mechanism. -- This policy is similar to the recycling mechanism in. net.

In the previous Excel operation sample code, the object owner is the process of "Excel. EXE ".
It can only happen after "Release of ActiveX object instance. File locks and operations
The system permission credential is related to the process. Therefore, if the object is "invalid" rather than "released ",
When other processes process files and reference operating system permission creden。, a problem occurs.

-- Some people say this is a JavaScript or com mechanism bug. Actually not. This is OS, ie
And JavaScript.

Microsoft published a policy to solve this problem: actively calling the memory recycle process.

A collectgarbage () process (GC process) is provided in (Microsoft's) JScript ),
The GC process is used to clear the "invalid object loss Examples" in the current IE, that is, the destructor of the called object.

In the above example, the code for calling the GC process is:

  //  ---------------------------------------------------------  
// Standard calling method for GC when processing ActiveX objects
// ---------------------------------------------------------

Function Writexls (){
// (Omitted ...)

Excel. Quit ();
Excel = Null ;
SetTimeout (collectgarbage, 1 );
}

The first line of code calls the Excel. Quit () method to stop and exit the Excel process.
The environment has an Excel object instance, so the Excel process does not actually stop.

The second line of code sets EXCEL to null to clear object references and invalidate the object ". However
The object is still in the function context. Therefore, if the GC process is called directly, the object will not be cleared.

The third line of code uses setTimeout () to call the collectgarbage function. The interval is set to '1', only
The GC process occurs after the writexls () function is executed. In this way, the EXCEL object can be
Two conditions for GC cleaning: no reference and exit from the context.

The use of GC is very effective in the JS environment where ActiveX object is used. Some potential ActiveX
Objects include XML, VML, owc (Office Web componet), Flash, and even vbarray in Js.
From this point of view, the Ajax architecture adopts XMLHTTP and must meet the requirement of "no page switching"
Feature, so actively calling the GC process when appropriate will get a better UI experience of efficiency.

In fact, even if the GC process is used, the Excel problem mentioned above will not be completely solved. Because IE also
Permission creden are cached. The only way to update the page's permission creden is to "switch to a new page ",
In fact, in the SPS project mentioned above, the method I used is not GC, but the following
Segment code:

  //  ---------------------------------------------------------  
// Page switching code used to process ActiveX objects
// ---------------------------------------------------------

Function Writexls (){
// (Omitted ...)

Excel. Quit ();
Excel = Null ;

// The following code is used to solve a bug in IE call excel, which is provided in msdn:
// SetTimeout (collectgarbage, 1 );
// Because the trusted status of (or synchronous) web pages cannot be cleared
// Invalid next call.
Location. Reload ();
}

Instructions on the delete operator in the manual

Reference
Deletes an attribute from an object or an element from an array.

Delete expression

The expression parameter is a valid JScript expression, usually an attribute name or an array element.

Description
If expression results in an object and the attribute specified in expression exists, and the object cannot be deleted, false is returned.

Returns true in all other cases.

Finally, a supplementary description about GC: When the IE form is minimized, ie will actively call
Collectgarbage () function. This makes the memory usage significantly improved after the IE window is minimized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.