Turn "JS Closure and Memory Leak"

Source: Internet
Author: User

first, there must be variables of reference types , such as functions and other custom objects, that can cause a memory leak. A variable of value type does not have a memory leak, such as a string, a number, a Boolean value, and so on.
Because value types are passed by replication, reference types are passed by pointers similar to those in the C language.
A variable of a reference type can be thought of as a pointer to a specific memory address .

When we create a reference type with the JS code (hereinafter referred to as the object), the JS engine will open a space in memory to hold the data, and the pointer reference to the variable. Memory is limited, the JS engine must ensure that when the open object is useless, the allocated memory space is released, this process is called garbage collection , is responsible for recycling called Garbage collector (GC).

OK, a memory leak means that we can no longer refer to an object through the JS code, but the garbage collector thinks the object is still being referenced, so it will not be released when it is recycled . The memory that caused the allocation will never be released. If this happens more and more, it can lead to insufficient memory and the system crashes.

What is not controllable is the most horrible!

The most classic example is an external reference that we cannot control. For example, the DOM object in IE6 refers to the JS object, and the DOM object is removed at some point, but the JS engine does not know that it is removed, but also innocently retains the reference, will not be the JS object release. (ie7+ improved a lot, I'm not black IE)

Then there is the reference in the closure. The purpose of our closures is to preserve the state of the internal variables so that we can use the variables within the scope of the closure.

We can understand the closure image as a door, inside the house is the internal variables. The key is a reference.
When we gave the key to Zhang San this object (OTHEROBJECT1.P1-door), a reference was produced
When we get a key to John Doe, this object (OTHEROBJECT2.P2-door) produces another reference.

The GC will determine when a closure is being recycled. If there is no one holding the key, if there is no reference or internal circular reference (John Doe in the room), it will release the space of the variable inside the closure, recycling garbage

I venture to say: Strictly speaking, closures are not really the cause of memory leaks! as you can say in your comments, here is a simple example:

function bindEvent() {     var obj = document.createElement("XXX"); obj.onclick = function(){ // ... } }bindEvent();

The man gave the key (citation) to an externally-controlled DOM object, how can I blame someone else for the mistake of closing the bag?
Then look at the following code:

var otherJsObj = {};function bind() { otherJsObj.func1 = function(){ // ... } }bind();

I gave the key to Otherjsobj. Then told it: "You do not need to put your func1 empty or assigned to other objects, lift my reference, I am good garbage collection". This is controllable, because we are all our own (JS object), have access to [hehe]

Landlord will create such confusion, because closures are really keeping references to other objects. Also produces a larger memory footprint. But this is controllable, not the fault of closures.

Turn "JS Closure and Memory Leak"

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.