A novice who loves technology ... With a bit of accumulation to cast tomorrow's talent
BodyIf you really want to learn a language, then must understand its memory model, this article will take you into the JavaScript memory model, because I caishuxueqian, if there is any wrong expression, welcome crossing can guide twos, here is grateful ... Before reading this article, by default you have mastered the basic concept of JavaScript, stack heap and other basic data structure and basic computer theory, if there is a lack of understanding, please go to the relevant blog post and then read this article.a basic data type of memory structureFirst of all, a rough introduction to JavaScript in five basic data types undefined, NULL, Boolean, number, String, where the difference between undefined and Null, there are many online Daniel has introduced, in this article is not involved, If you have any doubts, please visit the relevant blog. Perhaps some crossing ask why the memory structure of JavaScript's basic data type is introduced before the JavaScript memory model is introduced. This is because the relationship between the JavaScript memory model and the memory structure of the underlying data type is like the relationship between math and real numbers, and the memory structure of the basic data type is the basis of the entire JavaScript memory model. So let me explain the memory structure of the basic data type in the shortest way. The memory structure of the basic data type: In JavaScript, the basic data types are stored in memory in the form of values. As an example:
var inta = 10;var strb = ' Hello ';
Then after executing this JavaScript code, two areas in memory are represented as INTA,STRB, which indicates that the value of the Inta region is ' 10 ', indicating that the value of the STRB region is ' Hello ', which means that the memory area of INTA and STRB is the actual truth value;
Second, the memory structure of the reference data type
In JavaScript, in addition to the basic data types, there is the reference data type, so after the introduction of playing the basic data type memory structure, it is necessary to introduce the reference data type memory structure. The real object of the reference data type is stored in heap memory, and JavaScript is similar to Java, so it is not possible to access the heap memory directly, so we use "reference" to access the objects in the heap, and the relationship between the reference and the object can be described as the relationship between the remote control and the television. We can hold the remote to control the TV. The so-called reference is actually the address of a piece of memory, that is, the memory address value of an in-memory object is saved on the area that represents the reference:
Where the object is in an area called 0x23215 in memory, the area of the ellipse represents the object's reference, and the ellipse region is the value of 0x23125, and in the actual operation the execution environment will find the object in memory by referencing the 0x23125 stored in it.
Three, the memory model
During JavaScript execution time, memory can be logically divided into two parts: stacks and heaps. Where the stack is when JavaScript executes, it is used to store the execution context (as described in subsequent articles), and the heap is the area where the object is stored. After the context generation is executed, a variable object is created (as the following article describes), and the variable object is a special object, which is also stored in the heap. The basic data types tend to be stored directly in the variable object, whereas the reference data type actually holds a reference to the object's address (that is, the reference itself) in the variable object.
After learning the memory model in JavaScript, please crossing look at the following code and guess its output to verify the understanding of the above knowledge:
var inta = 10;var stra = ' Hello '; var obja = {a:10, b: ' Hello '};var intb = Inta;var STRB = Stra;var OBJB = OBJA;INTB = 20; STRB = "World"; objb.a = 20;objb.b = ' World '; Console.log (INTA, INTB); Console.log (Stra, STRB); Console.log (Obja, OBJB);
Operation Result:
20Hello world{a:20, B: ' World ' {a:20, B: ' World '}
This involves the assignment of objects, when assigning a value to a base data type, the original value is assigned to the new object, so changing the value of the new object does not affect the original value (since they essentially hold two values), whereas assigning a value to a reference data type assigns the address of the Reference object to another reference , and in subsequent operations, if the values inside the object are changed by a new reference, the object pointed to by the original reference is affected (because they essentially hold the same object)
Iv. Memory Recovery mechanism
JavaScript has an automatic garbage collection mechanism, which means that the execution environment is responsible for the memory used during the execution of the Code, periodically (periodically) identifying which objects are no longer in use, and then releasing their memory. Currently, the most commonly used garbage collection algorithm for JavaScript is the markup cleanup algorithm, which uses the tagged algorithm to determine which objects do not need to be reused, and then cleans up those JavaScript objects that are defined as garbage.
The variables that are no longer used as described above, which are the end-of-life variables, are, of course, only local variables. The life cycle of the global variable will not end until the browser unloads the page, so when declaring a global variable, we must consider carefully whether we still need the object after using the variable object, and if not, we should manually set the variable to NULL, This allows the next garbage collection to be released to the object (note the difference between the variable and the object).
Below please crossing see the following code, to analyze garbage collection.
function fun1 () { var obj = {name: ' CSA ', age:24};} function fun2 () { var obj = {name: ' Coder ', age:2} return obj;} var f1 = fun1 (); var F2 = fun2 ();
In the above code, when executing var f1 = fun1 (), the execution environment creates a {name: ' CSA ', age:24} object, when executing var F2 = fun2 (), the execution environment creates a {name: ' Coder ', age=2} This object, and then the next time garbage collection comes, will release {name: ' CSA ', age:24} This object's memory, but will not release {name: ' Coder ', age:2} This object's memory. This is because the {name: ' Coder, Age:2 '} object is returned in the fun2 () function, and its reference is assigned to the F2 variable, and because the F2 object is a global variable, F2 points to the object {name: ' coder ' If the page is not unloaded, Age:2} is not recycled.
Because of the specificity of the JavaScript language (closures ...), it is difficult to determine whether an object will be recycled, which not only requires strong basic skills, but also accumulates experience in future projects. Finally, by the way, even with the upgrading of hardware and the improvement of garbage collection mechanism, we should not neglect the basic theory of garbage collection, because this is a key step to improve the performance of code, so that we can write those called Art code ...
The memory model of the JavaScript learning series