Memory space management

The memory life cycle of JavaScript is

  1. Allocate as much memory as you need
  2. Use allocated memory (read, write)
  3. Release and return them when they are not needed

JavaScript has an automatic garbage collection mechanism. The most commonly used method is to find out which objects are no longer in use through the tag clearing algorithm. Using a = NULL actually just does a reference release operation, making the corresponding value of A lose reference and out of the execution environment. This value is found and freed the next time the garbage collector performs an operation.

In a local scope, when the function is finished executing, there is no need for local variables to exist, so it is easy for the garbage collector to make judgments and reclaim. However, it is difficult to determine when global variables need to automatically free memory space, so avoid using global variables as much as possible during development.

In JS, each piece of data requires one memory space. Memory space is divided into two types, stack memory and heap memory.

The stack (stack)

The stack structure is lifO. Here’s the structure of a ping-pong ball box:

Ping-pong ball 5, on the top of the box, must be the last to go in, but can be used first. And if we want to use the bottom ping-pong ball 1, we have to take the top four ping-pong balls out and put ping-pong ball 1 on top of the box.

Heap (heap)

The heap data structure is a tree structure. It accesses data in much the same way as bookshelves and books. Just need to know the name of the book can be directly to the book, there is no need to take out the book above. In JSON format data, key-values can be unordered, because the difference in order does not affect the use, only the name of the book is concerned.

Variable for

  • Basic types –> are stored in stack memory, because these types occupy a fixed amount of memory and are accessed by value.
  • Reference types –> are stored in heap memory, because they are not fixed in size, so they cannot be stored in stack memory, but memory addresses are fixed in size, so they are stored in stack memory, where only the access address of the object is stored. When querying a variable of reference type, the memory address is read from the stack and then the value in the heap is found by the address. For this, we call it access by reference.

Better understand through code and diagrams:

// Primitive types are placed in the stack
// Reference types are placed in the heap
var a = 10;
var b = 'lzm';
var c = true;
var d = { n: 22 }; // If the address is 0x0012FF7f, it does not represent the actual address
var e = { n: 22 }; // Create a new segment of memory with an address of 0x0012ff8c
console.log(e==d); //false
var obj = new Object(a);// Assume the address is 0x0012ff9d
var arr = ['a'.'b'.'c']; // Address is assumed to be 0x0012ff6e
Copy the code

In the data structure of computer, stack is faster than heap. Object is a complex structure and can be expanded: array can be expanded, Object can add attributes, and can be added, deleted, changed, and searched. They are placed in the heap so as not to affect stack efficiency. Instead, we refer to the actual object in the heap, so when we look for a reference type value, we look on the stack first and then on the heap.

A few questions

Question 1

var a = 1;
var b = a;
b = 2;

// What is the value of a?
Copy the code

As can be seen from the figure above, the answer is: 1. When data in stack memory is replicated, the system automatically assigns a new value to the new variable. Var b = a; var b = a; var b = a;

Question 2

var a = {name: 'a'} 
var b = a 
b.name = 'b' 

// What is a.name now?
Copy the code

As you can see from the figure above, the answer is: “B”. We perform a copy of the reference type with var b = a. The copy of the reference type also automatically assigns a new value to the new variable in stack memory, but this new value is only an address pointer to the reference type. When the address Pointers are the same, even though they are independent of each other, the specific objects accessed in the heap memory are actually the same, so b.name =’b’ changes the value of the object in the heap memory, and the value of A. name changes accordingly.

Question 3

var a = {name: 'a'} 
var b = a 
b = nullWhat is A nowCopy the code

As you can see from the figure above, the answer is {name:”a”}. Because null is a base type, it is stored in stack memory. So variable B in stack memory is null, and variable A’s address is still pointing to the original object.

conclusion

Stack memory Heap memory
Store basic data types Store reference data types/or underlying data types, such as data in closures
According to the value of access Access by reference
Values stored are fixed in size Stored values vary in size and can be adjusted dynamically
The system automatically allocates memory space It is assigned by the programmer through code
Mainly used to execute programs Mainly used to store objects
Small space, high operating efficiency Large space, but relatively low operating efficiency
Last in, first out Unordered storage, which can be obtained by reference

Reference: juejin. Cn/post / 684490… Muyiy. Cn/blog / 1/1.3….