This is the 24th day of my participation in the August More Text Challenge

Js memory allocation mechanism

Js memory space is divided into: stack, heap, pool, stack for storing variables, heap for storing complex objects, pool for storing constants.

  • Js basic data types. These values have a fixed size and are usually stored on a stack (except for closures), with storage allocated automatically by the system. We can directly manipulate the values stored in the stack memory space, so the basic data types are accessed by value. The storage and use of data in the stack memory is similar to the stack data structure in the data structure, following the last in, first out principle.

  • Reference data types vs. heap memory: The size of reference data types in JS is not fixed. Objects stored in heap memory are referenced when a value of a reference data type is referenced. Js does not allow direct access to locations in heap memory, so we cannot directly manipulate the heap memory space of objects. When you manipulate an object, you’re actually manipulating a reference to the object rather than the actual object. An address stored in stack memory is associated with the actual value of heap memory.

About the life cycle of memory

  1. Memory allocation: When we request variables, functions, and objects, the system automatically allocates memory for them
  2. Memory usage: that is, reading and writing memory, that is, using variables and functions
  3. Memory reclamation: The garbage recycle mechanism reclaims unused memory

Js garbage collection mechanism

JS has a memory management engine, which is responsible for object creation, object destruction, and garbage collection. This time we’ll look at the garbage collection mechanism. The garbage collection mechanism consists of a background process called the Garbage Collector (GC) that monitors, cleans up objects, and reclaims free memory in a timely manner.

The necessity of recycling

Each time an entity is created, memory is allocated dynamically, which, if not freed, can consume all available memory in the system, causing the system to crash.

Garbage collection method

There are two ways for the garbage collector in the JS execution environment to detect which chunk of memory can be reclaimed: Mark and sweep and Reference counting.

Mark and sweep

The garbage collector marks a variable as “in” when it enters the execution environment (a variable declared in a function), “out” when it leaves the environment (the function ends), and any variables that remain after the exit environment are variables that need to be deleted. The notation varies, from the inversion of a particular bit to the maintenance of a list.

The garbage collector marks all variables in memory and then removes the marks of variables in the environment and those referenced by variables in the environment. Variables tagged after this point are the ones that need to be reclaimed because they are no longer accessible to variables in the environment.

Function func() {const a = 1 const b = 2} func() {const a = 1 const b = 2Copy the code

Reference Counting (Reference Counting)

According to the number of citations, when declare a variable and will be a reference type assigned to the variable, this is worth reference number is 1, on the contrary, if contain reference to the value of variables and made another value, it is worth reference number is 1, when this is worth reference number into 0 means that there is no way to access the value again, So you can reclaim the memory space it takes up. In this way, the next time the garbage collector runs, it will free the memory occupied by those zero references.

This approach often causes memory leaks and is used in earlier versions of IE. This approach causes memory leaks because it does not solve the problem of circular references

function sample(){
    var a={};
    var b={};
    a.prop = b;
    b.prop = a;
}
Copy the code

In this case, the reference count for both a and B is 2 each time sample() is called, so that this portion of memory will never be freed, i.e. a memory leak.

conclusion

JS garbage collection mechanism is to prevent memory leakage, which means that a chunk of memory still exists when it is no longer needed. Garbage collection mechanism is to find variables that are no longer used and release the memory they point to intermittently and irregularly.