The life cycle of memory

Regardless of any language, the memory life cycle is roughly the same, which can be divided into three phases: allocating memory, using memory, and destroying memory

Distribution of 1.

1.1 Static Memory Allocation

1.2 Dynamic Memory Allocation

The difference between:

Static memory allocation:

  • The data size must be known at compile time
  • Execute at compile time
  • Assigned to the stack
  • After the advanced

2. Use

Operations such as reading and writing basic variables or attributes of objects and passing parameters all involve the use of memory.

Release 3.

The memory that is no longer used should be released in a timely manner.


V8 memory structure

The running program is represented by some memory that becomes a resident set

1. The stack memory

The stack is used for Static Memory Allocation and has the following characteristics:

  • Manipulating data is fast because you operate at the top of the stack
  • The data must be static and the size of the data is known at compile time
  • In multithreaded applications, each thread can have one stack
  • Memory management for the heap is simple and done by the operating system
  • The Stack size is limited and Stack Overflow may occur.
  • The value size is limited

2. The heap memory

The heap is used for Dynamic Memory Allocation and, unlike the stack, the program needs to use Pointers to find data in the heap. Its features are:

  • Slow operation speed, but large capacity
  • This is where dynamically sized data can be stored
  • The heap is shared between the threads of the application
  • Heap management is difficult because of the dynamic nature of the heap
  • There is no limit to the value size

The memory of the heap can be further divided:

1. New generation: small and divided into two half-spaces managed by Minor GC, where data lifetime is short

2. Old generation: Large space, managed by Major GC, old generation can be further divided into:

  • Old pointer space: Contains objects with Pointers that point to other objects

  • Old data space: Contains objects that have only data

3. Large object space: Here the object size exceeds other space size limits

4. Code space: This is where the just-in-time compiler stores compiled code blocks

5. Meta space, attribute meta space, mapping space: Each of these Spaces contains objects of the same size and has some constraint on the objects they point to, simplifying collection.

(Page: A contiguous block of memory allocated from the operating system. The above space is made up of groups of pages.)


Reclaim stack memory

V8 destroys the execution context stored in the stack by moving the pointer that records the current execution state


Reclaim heap memory

The Garbage Collector in V8 keeps track of the allocation and use of memory so that when allocated memory is no longer used, it is automatically freed. Also, this garbage collector is generational, that is, objects in the heap are grouped by age and purged at different stages.

There are two ways to reclaim heap memory:

1. Reference counting

2. Mark removal

In V8, two phases and three algorithms are used for GC:

1.Minor GC: Scavenger and Cheney’s algorithm are used for the Cenozoic generation

2.Major GC: For older generations, the Mark-sweep-Compact algorithm is used

Two ideas:

1. Reference counting method:

Memory References is an important concept in reference counting. In the context of memory management, an object is said to reference another object if it can implicitly or explicitly access another object. For example, JavaScript objects can reference their archetypes (implicit references) and the values of their properties (explicit references).

Reference counting is when an object’s reference count is zero, that object is treated as recyclable garbage

Disadvantages: Garbage collection is not possible if circular references occur.

function f() {
  var o1 = {};
  var o2 = {};
  o1.p = o2; // o1 references o2
  o2.p = o1; // o2 references o1. This creates a cycle.
}

f();
Copy the code

2. Mark removal

Instead of reducing the question of whether an object is no longer needed to whether an object is referenced, tag cleanup starts at the root node, marks reachable nodes (tag), and then releases untagged nodes (cleanup).

The token cleanup method solves the problem of circular references. In the previous example code, after the function call returns, two objects are unreachable from the global object. Therefore, they will be collected by the garbage collector.

Const and let have block-level scope. When the block-level scope disappears, memory can be reclaimed in time to improve program efficiency

Two steps of V8 GC

1.Minor GC

The Minor GC is garbage collection for nascent areas. The whole idea of Minor GC :(this process uses Scavenger and Cheney’s algorithm.)

  • The new generation is divided into two halves, namely “to-space” and “from-space”, where we continuously allocate memory
  • If from-space is full, GC is triggered
  • Find live objects on from-space and move them To the old generation if they survive two minor GC cycles, otherwise move them To to-space
  • To empty the from Space
  • Switch the roles of “to-space” and “from-space”
  • Repeat the process

2.Major GC

Scavenger algorithm needs to involve data migration, so it is suitable for small data, but it is not suitable for large data in old areas.

The Major GC is a mark-sweep-compact garbage collection algorithm for old areas.

  • Marking: Depth-first traversal of the heap, marking all reachable objects on it
  • Clear: The memory addresses of all unmarked objects are free memory space that can be used to store other objects
  • Compression: Move all living objects together to reduce fragmentation and improve the performance of allocating memory for new objects

Stop-the-world GC: This type of GC is also referred to as total pause because JavaScript runs on the main thread. Once the garbage collection algorithm is executed, the JavaScript script that is being executed needs to be paused, and the script execution can be resumed after garbage collection, resulting in performance degradation.

Resolution strategies adopted in V8:

1. Incremental programming: GC is done in multiple incremental steps, not all at once

2. Concurrent marking and concurrent cleanup/compression: This is done concurrently using multiple helper threads without affecting the main thread

Lazy cleanup: Deferred processing of garbage in a Page until memory is needed to clean it up.


How to optimize the use of memory

1. Do not define functions in loops

2. Do not define objects in loops

3. Clear the array

arr.length=0;
Copy the code

Learning links:

Juejin. Cn/post / 697124…

Juejin. Cn/post / 689161…