Tutorial on management and release of JavaScript memory

Source: Internet
Author: User

An instance of memory release

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



CollectGarbage, a unique property of IE, used to free memory
Use the method should be, the variable or reference object, set to null or delete
And then you're doing the release action.
Before you make a collectgarbage, you must have a 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).

  code is as follows copy code
//------------ ---------------------------------------------when
//JavaScript Object expires
//-------------------------------------- -------------------
Function Testobject () {
var _obj1 = new Object ();
}
 
Function TestObject2 () {
var _obj2 = new Object ();
return _obj2;
}
&NBSP
//Example 1
testobject ();
 
//Example 2
TestObject2 ()
 
//Example 3
var obj3 = Test Object2 ();
Obj3 = null;
&NBSP
//Example 4
var obj4 = TestObject2 ();
var arr = [Obj4];
obj3 = null;
arr = [];



In these four examples:
-"Example 1" constructs _obj1 in function testobject (), but when the function exits,
It has left the context of the function, so the _obj1 is invalid;
-"Example 2", TestObject2 () also constructs an object _obj2 and is outgoing because the
object has a contextual environment (and a lifetime) outside of the function, but because the return value of the function
is not "held" by another variable, the _obj2 is immediately invalidated;
-"Example 3" , the _obj2 of the TestObject2 () construct is used by the external variable obj3,
at which point _obj2 will not expire because the reference relationship
disappears until the line of code in Obj3=null is in effect.
-for the same reason as the example 3, the _OBJ2 in Example 4 will not be invalidated until the "arr=[" line of code
.

However, the "invalid" of an object does not wait to be "freed". Inside the JavaScript runtime environment, there is no
any way to tell the user exactly when the object will be released. This relies on the memory recycle mechanism of JavaScript
. --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
can only occur after" release of an ActiveX object instance ". The lock of the file and the permission credentials of the
system are related to the process. So if the object is only "fail" instead of "release",
the other process will have problems processing files 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 policy to address this problem by actively invoking the memory recycle process.

provides a collectgarbage (usually referred to as a GC process) in JScript (Microsoft's), which 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:

  code is as follows copy code
//------------ ---------------------------------------------
//Handling ActiveX object, standard invocation method for GC procedure
//------------------------- --------------------------------
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 Excel object instance, the Excel process is not actually aborted.

The second line of code makes Excel null to clear an object reference, which causes 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 ', and only
to make the GC process occur after the Writexls () function has been executed. This allows the Excel object to satisfy the two conditions that can be cleaned by the
GC: no reference and no departure from the context environment. The use of the

GC procedure is effective in a JS environment that uses ActiveX object. Some potential ActiveX
object includes XML, VML, OWC (Office Web componet), Flash, and even vbarray included in JS.
From this point of view, the AJAX architecture is XMLHTTP and satisfies the "do not switch pages"
feature, so it is much more efficient to use the UI experience when the GC process is actively invoked at the appropriate time.

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

  code is as follows copy code
//------------ ---------------------------------------------the
//page switch code used when handling ActiveX object
//---------------------------- -----------------------------
Function Writexls () {
///(slightly ...)
 
Excel. Quit ();
Excel = null;
&NBSP
//The following code is used to resolve a bug in IE call Excel, as provided in MSDN:
//SetTimeout (CollectGarbage, 1);
//due to the inability to purge (or sync) the trusted Web page , so that methods such as SaveAs () will be invalidated at
//next invocation.
Location.reload ();
}

The



Delete operator in the manual description

Reference Deletes a property from the object, or deletes an element from the array. The

Delete expression

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.

Returns true in all other cases.


Finally, one additional note about GC: When IE forms are minimized, IE will actively invoke the
CollectGarbage () function. This allows the IE window to be minimized after the memory footprint is 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.