Tutorial on management and release of JavaScript memory

Source: Internet
Author: User

An instance of memory release

The code is as follows
<script language= "JavaScript" >
<!--
Strtest = "1";
for (var i = 0; i < i + +)
{
Strtest + = strtest;
}
alert (strtest);
Delete strtest;
CollectGarbage ();
-->
</script>

CollectGarbage, is a unique property of IE, used to free the memory of the use of the method should be, the variable or reference object, set to null or delete and then in the release action before doing collectgarbage, must be clear two prerequisites:

Reference-An object is invalidated outside the context in which it is living.

-A global object is invalidated if it is not being used (referenced).

The code is as follows
//---------------------------------------------------------
When JavaScript objects fail
//---------------------------------------------------------
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 the _obj1 in the function Testobject (), but when the function exits, it has left the context of the function, so the _obj1 is invalid;

-in Example 2, an object is also constructed in TestObject2 () and is _obj2, so that the object has a context (and a lifetime) outside of the function, but the _OBJ2 is immediately invalidated because the return value of the function is not "held" by another variable;

-In "Example 3", the _obj2 of the TestObject2 () construct is held by an external variable obj3, and _obj2 will not expire because the reference relationship disappears until the "obj3=null" line of code takes effect.

-For the same reason as Example 3, the _obj2 in "Example 4" will not expire until after the "arr=[" line of code.

However, the "expiration" of an object is not "released". Within the JavaScript runtime environment, there is no way to tell the user exactly when the object will be released. This relies on JavaScript's memory recycle mechanism. --This strategy and. NET, the recycling mechanism is similar.

In the previous Excel action sample code, the owner of the object, which is "Excel." EXE "This process will only occur after" release of an ActiveX object instance ". The lock on the file, and the permission credentials of the operating system, are related to the process. So if the object is only "fail" instead of "release", then the other process will have problems processing the file and referencing the permissions credentials of the operating system.

Some people say this is a bug in JavaScript or COM mechanics. Not really, this is the result of a complex relationship between OS, ie and JavaScript, rather than a stand-alone issue.

Microsoft exposes a strategy to address this problem by actively invoking the memory recycle process.

In JScript (Microsoft), a collectgarbage (usually referred to as a GC process) is provided, and the GC process is used to clean up "failed object loss cases" in current IE, which is the destructor of the calling object.

The code that invokes the GC procedure in the previous example is:

The code is as follows
//---------------------------------------------------------
Standard invocation method for GC process when handling ActiveX object
//---------------------------------------------------------
function Writexls () {
(Slightly ...)

Excel. Quit ();
Excel = null;
SetTimeout (CollectGarbage, 1);
}

The first line of code calls Excel. Quit () method to cause the Excel process to abort and exit because the JavaScript environment holds an instance of the Excel object, so the Excel process is not actually aborted.

The second line of code makes Excel null to clear the object reference, causing the object to "fail". However, because the object is still in the function context, the object will not be cleaned up if the GC procedure is called directly.

The third line of code uses settimeout () to invoke the CollectGarbage function, with the interval set to ' 1 ', just to make the GC process happen after the Writexls () function has been executed. This allows the Excel object to meet the two criteria for GC cleanup: No referencing and leaving the context.

The use of GC processes is effective in JS environments that use ActiveX object. Some potential activexobject include XML, VML, OWC (Office Web componet), Flash, and even vbarray that are included in JS. From this point of view, Ajax architecture because of the adoption of XMLHTTP, and at the same time to meet the "do not switch the page" feature, so the appropriate time to actively invoke the GC process, will be better efficiency with the UI experience.

In fact, even with the GC process, the Excel problem mentioned earlier will not be completely resolved. Because IE also caches the permission credentials. The only way to make the page's permission credentials updated is to "switch to a new page", so in fact, the method I used in the SPS project mentioned earlier is not a GC, but the following code:

The code is as follows
//---------------------------------------------------------
Page switch code used when handling ActiveX object
//---------------------------------------------------------
function Writexls () {
(Slightly ...)

Excel. Quit ();
Excel = null;

The following code resolves a bug in IE call Excel, which is provided in MSDN:
SetTimeout (CollectGarbage, 1);
Because the trusted state of the Web page cannot be purged (or synchronized), it will cause methods such as SaveAs () to
The next call is invalid.
Location.reload ();
}

Description of the delete operator in the manual

The reference deletes an attribute from the object, or deletes an element from the array.

Delete expression

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

Description

Returns false if the result of the expression is an object and the property specified in expression exists and the object does not allow it to be deleted.

In all other cases, returns TRUE.

Finally, a supplementary note about GC: When IE forms are minimized, IE will call the CollectGarbage () function once. This allows the IE window to be minimized, memory footprint will be significantly improved

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.