Detailed explanation of JavaScript Memory release issues, and javascript Memory release

Source: Internet
Author: User

Detailed explanation of JavaScript Memory release issues, and javascript Memory release

This article describes in detail the timing and methods for memory management and release by JavaScript and IE browsers, hoping to help front-end developers.

An instance with memory released

Copy codeThe Code is as follows:
<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 release the object.

Before you perform CollectGarbage, you must understand the following two prerequisites:

Reference-an object that is not in the context of its survival will become invalid.
-A Global object becomes invalid if it is not used (referenced.

Copy codeThe Code is as follows:
//---------------------------------------------------------
// 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 it leaves the context of the function when the function exits, SO _ obj1 becomes invalid;

-In "Example 2", testObject2 () also constructs an object _ obj2 and transmits it out. Therefore, the object has an "out-of-function" Context Environment (and lifecycle ), however, because the return value of the function is not "held" by other variables, _ obj2 also becomes invalid immediately;

-In Example 3, The _ obj2 constructed by testObject2 () is used by the external variable obj3 until the code of "obj3 = null" takes effect, _ obj2 will expire because the reference link disappears.

-For the same reason as Example 3, _ obj2 in "Example 4" will expire after "arr =.

However, the "invalid" of the object will not be "released ". In the JavaScript runtime environment, there is no way to tell the user exactly when the object will be released ". This depends on the memory collection mechanism of JavaScript. -- This policy is similar to the recycling mechanism in. NET.

In the previous Excel operation sample code, the Object owner, that is, the process "EXCEL. EXE", can only occur after the "ActiveX Object instance is released. File locks and operating system permission creden。 are related to processes. Therefore, if the object is "invalid" rather than "released", other processes may encounter problems when processing files and referencing permission creden。 of the operating system.

-- Some people say this is a JavaScript or COM mechanism BUG. Actually not. This is caused by a complex relationship between OS, IE, and JavaScript, rather than independent issues.

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 "invalid object loss Examples" in IE ", that is, the destructor of the called object.

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

Copy codeThe Code is as follows:
//---------------------------------------------------------
// Standard GC call method for processing ActiveX objects
//---------------------------------------------------------
Function writeXLS (){
// (Omitted ...)
Excel. Quit ();
Excel = null;
SetTimeout (CollectGarbage, 1 );
}

The first line of code calls the excel. Quit () method to abort and exit the excel process. At this time, the excel process is not actually aborted because the JavaScript environment has an excel object instance.

The second line of code sets excel to null to clear object references and invalidate the object ". However, because the object is still in the context of the function, 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', but the GC process occurs after the writeXLS () function is executed. In this way, the excel object meets the two conditions of "GC cleanup": 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 activexobjects include XML, VML, OWC (Office Web Componet), flash, and even VBArray in JS. From this point of view, because the ajax architecture adopts XMLHTTP and must meet the "Do not switch pages" feature, it actively calls the GC process when appropriate, it will get a better efficiency with the UI experience.

In fact, even if the GC process is used, the excel problem mentioned above will not be completely solved. Because IE also caches permission creden. The only way to update the page's permission creden is to "switch to a new page ",

In fact, in the preceding SPS project, the method I used is not GC, but the following code:

Copy codeThe Code is as follows:
//---------------------------------------------------------
// Page switching code used to process ActiveX objects
//---------------------------------------------------------
Function writeXLS (){
// (Omitted ...)
Excel. Quit ();
Excel = null;
// The following code is used to solve an IE call Excel BUG. the method provided in MSDN:
// SetTimeout (CollectGarbage, 1 );
// Because the trusted status of (or synchronous) web pages cannot be cleared, methods such as SaveAs () are
// It is invalid for the next call.
Location. reload ();
}

Instructions on the delete operator in the manual
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 take the initiative to call the CollectGarbage () function. This makes the memory usage significantly improved after the IE window is minimized.

Related Article

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.