JS Memory Knowledge Points Summary

Source: Internet
Author: User

Nonsense film:

Talk about memory can be associated with a lot of memory allocation management, memory recovery mechanism, memory leaks, and so on, today we will answer the summary.

Body piece:

1. Memory allocation Management

2. Memory recovery mechanism

3. Memory leaks

One, memory allocation management

First, let's look at the memory allocation mechanism. In fact, in all languages the allocation of memory is the same, the first time we need to remember the memory allocation, then the use of allocated memory, and finally when we do not apply the time to recover the memory block.

  question: How to allocate memory in JS, when to allocate it?

  Answer: in JS, when we define the variable, we actually allocate the memory, which will save us a lot of trouble. as follows:

1 //allocating memory to numeric variables2 varn = 123; 3 //allocating memory to a string4 vars = "Azerty"; 5 6 //allocating memory to an object and its contained values7 varo = {8A:1,9B:NULLTen };  One  A //allocates memory for an array and the values it contains (just like an object) - varA = [1,NULL, "Abra"];  -  the //allocating memory to a function (callable object) - functionF (a) { -   returnA + 2; - }  +  - //function expressions can also be assigned an object +Someelement.addeventlistener (' click ',function(){ ASomeElement.style.backgroundColor = ' Blue '; at},false);

Of course, there are other things that actually require the system to allocate memory for us, for example, when we use constructors to define a new object, we need to beg for space for the new object.

1 var New // assign a Date object 2 var // Assigning a DOM element

Sometimes we need to combine the data or set the value on the original data, and the system will allocate a new memory area for us.

1 vars = "Azerty";2 varS2 = s.substr (0, 3);//S2 is a new string3 //because the string is a non-variable4 //JavaScript may not have memory allocated5 //but only the range of [0-3] is stored. 6 7 varA = ["Ouais Ouais", "Nan nan"];8 varA2 = ["Generation", "Nan nan"];9 varA3 =A.concat (A2);Ten //The new array has four elements, which is the result of a connection A2

  problem: memory space usage?

  Answer: When the system allocates memory space for us, we actually use this block of memory every time it is worth the operation, and the read and write operations will be our underlying operation on the memory block.

Second, memory recovery mechanism

  question:How is js memory recycled?

  Answer:JS is an automatic garbage collection mechanism, this mechanism will automatically track the movement of each variable, and determine whether the current variable is still necessary to exist, and then the unnecessary variables occupied by the memory of the recovery. There are two different ways to realize this kind of recovery mechanism, which is actually used in practice. See below for details:

  1. Method One: Mark clear algorithm

This algorithm marks a "go to environment" tag for variables entering the environment. Logically, when our variables enter the environment, the variables actually should not be deleted, because the context may use the current variable for the relevant logic calculus, and when the variable leaves the environment, it should be marked as "out of the environment" state.

  2. Method Two: reference counting

The implication is that Geng always records the number of times each variable is called. When the count of variables becomes 0, it means that we will not be in the position of the current variable, this is the time to take back the memory space.

The above is actually the most commonly used memory recovery mechanism, of course, our memory recovery mechanism in a certain interval after the automatic operation, each time will be searched for the variable can be retracted, and reclaim memory.

Third, memory leaks

Memory leak refers to the process of our coding, some operations are the current block of memory even if it is not in use, the release mechanism is also because the identification (bug) does not need to be released to make this unnecessary variable is retained. Although this is rarely the case with JS and Java, which have a memory-recycling mechanism. But... You know, here's a look at what's going to lead to this situation

1. When we use in IE. onclick if we do not manually delete this event will cause and memory leaks (ie really good pit AH)

1document.getElementById ("xxx"). onclick =fuction () {};2 //in IE, the memory is not automatically recycled. 3 4 //Improvement Methods5document.getElementById ("xxx"). onclick =fuction () {6document.getElementById ("xxx"). onclick =NULL;7     .......8 };9 Ten //or use Onedocument.getElementById ("xxx"). AddEventListener (...);

2, when we use a circular reference to the DOM object, JS Recycling mechanism will not be able to recycle.

1 var a = document.getElementById ("xxx"); 2 A.R = A;

3, must be reported in the process of writing is often used to a concept. But when we define the response time in the closures, we create a certain amount of memory consumption.

1 function  23     var obj=document.createelement ("XXX"4     obj.onclick=function 5         //  67    }// The obj element in such a closure is returned because of the binding of the onclick time in it and there is no way to release it. This creates a waste of memory at run time. 

4, some code is not rigorous may lead to memory leaks

1 a = {p: {x:1}}; 2 b = a.p; 3 Delete a.p; the elements in the//a have actually been deleted, but the deleted element is not emptied because the external variable B points to the element data. This could lead to a waste of memory.

5, finally, we are in the black ie a handful, in fact, there are some dom operations in IE or the operation of property conversion will cause the risk of related memory leaks, so we have to write in the adaptation of IE browser is more attention to some of the AH.

The case of memory leaks is definitely more than what I said above, in our usual process of writing code, we can find more such bugs and related to the optimization, will make our code more good and robust.

  

JS Memory Knowledge Points Summary

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.