About JS closures will actually cause a memory leak (reprint)

Source: Internet
Author: User

Closures are a very powerful feature, but there are also many solutions to them. An alarmist remark is that closures can cause memory leaks.

A local variable should be dereferenced when the function exits, but if the local variable is enclosed in an environment where the closure is formed, the local variable can survive forever. In this sense, closures do make some data impossible to destroy in time. Part of the reason for using closures is that we choose to proactively hold some variables in the closures, because they may need to be used later, put them in closures, and put them in the global scope, and the memory impact is consistent, and this cannot be described as a memory leak. If we need to recycle these variables in the future, we can manually set these variables to null.

In the case of closures and memory leaks, it is easy to form circular references while using closures, which can cause memory leaks if some DOM nodes are kept in the scope chain of closures. But this is not a problem of closures in itself, nor is it a question of JavaScript. In IE, because objects in the BOM and Dom are implemented using C + + as COM objects, the garbage collection mechanism of COM objects uses a reference counting strategy. In the garbage collection mechanism based on the reference counting policy, if a circular reference is formed between two objects, neither object can be reclaimed, but the memory leaks caused by circular references are not inherently closed.
Javascript garbage collection mechanism, I now know that there are two kinds: tag, count.

Tag cleanup: Mainstream policy and is not related to this issue.

Reference count: A policy that is prone to problems when circular references occur. Because the count records the number of times that are referenced, the count of circular references is not eliminated. Causes the memory to not be freed. IE 9-The problem is that in the environment the BOM and Dom are not native JS objects, but COM objects, and the garbage collection mechanism of COM objects is a reference counting policy. In other words, as long as there is a COM object in IE, there is a circular reference problem. Like what

var Element=document.getelementbyid ("Someelement");
var myobject=new Object ();
Myobject.element=element;
Element.someobject=myobject;

In this example, a circular reference is established between the JS object and the DOM object, and because of this circular reference, even if the COM object is removed from the page, it will never be recycled. To avoid similar problems, you should manually disconnect the JS object and the COM object when you do not use them.

Myobject.element=null;
Element.someobject=null;

IE9 's previous browsers use garbage collection mechanisms that are not used by JavaScript objects and COM objects, so closures can cause some special problems in these versions of IE
Like what

function Assignhandler () {
var Element=document.getelementbyid ("Someelement");
Element.onclick=function () {
Alert (element. ID);
}
}

The code above creates a closure that acts as an element event handler, and the closure creates a circular reference, because the anonymous function saves a reference to the active object of Assignhandler (), thus causing the reference number of element to not be reduced, so As long as the anonymous function exists, the element reference is at least 1, so the memory can not be recycled, to be changed to resolve

function Assignhandler () {
var Element=document.getelementbyid ("Someelement");
var id=element. Id;

Element.onclick=function () {
alert (ID);
};
Element=null;
}

By putting element. A copy of the ID is stored in a local variable, and the reference to change in the closure is used to eliminate the circular reference, but only this step does not eliminate the memory leak, because the closure refers to the entire active object containing the function, which contains element, even if it is not directly referenced, A reference is still held in the active object that contains the function, so it is necessary to set the element variable to null.


Similarly, if you want to solve the memory leak problem caused by circular references, we just need to set the variable in the circular reference to NULL. Setting a variable to null means cutting off the connection between the variable and the value it previously referred to. The next time the garbage collector runs, the values are removed and the memory they occupy is reclaimed.

About JS closures will actually cause a memory leak (reprint)

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.