Complete high-frequency question bank warehouse address: github.com/hzfe/awesom…

The full frequency question bank reading address: febook.hzfe.org/

Issues related to

  • What is a memory leak

  • Common garbage collection algorithms

  • How do I troubleshoot memory leaks

Answer key points

Reference counting method mark-compact

The Garbage Collection (GC) is an automatic memory management mechanism. The Garbage Collector can automatically reclaim the memory allocated to the program that is no longer used. Common GC algorithms include reference counting method and mark removal method. The garbage collector algorithm for V8 (the JavaScript engine that provides a runtime environment for executing JavaScript) consists mainly of Mark-Compact and Scavenger.

Knowledge depth

1. Memory leaks

A memory leak means that objects that should be recycled are not recycled properly and become objects that reside in the old generation, resulting in higher and higher memory usage. Memory leaks can cause application slowdowns, high latency, crashes, and more.

1.1 Memory Life Cycle

  1. Allocation: Allocates memory on demand.

  2. Use: Read and write allocated memory.

  3. Free: Free memory that is no longer needed.

1.2 Common causes of Memory Leakage

  • Global variables are created without manual collection.

  • Event listeners/timers/closures etc. are not cleaned properly.

  • Use JavaScript objects for caching and do not set expiration policies or object size controls.

  • Delayed consumption caused by queue congestion.

2. Reference Counting

Reference Counting is a common garbage collection algorithm. Its core idea is to save the number of references of resources (such as objects) and release them when the number of references reaches zero. Limitations of this approach: When circular references occur, objects that refer to each other are not recycled.

V8 garbage collection mechanism

There are two garbage collectors in V8. The main GC uses the Mark-Compact garbage collection algorithm to collect garbage from the entire heap. Small GC uses Scavenger garbage collection algorithm to collect new generation garbage.

Two different algorithms deal with different scenarios:

  • The Scavenger algorithm is mainly used to deal with accessible objects in objects with short lifetime.

  • The Mark-Compact algorithm is mainly used to deal with unreachable objects in objects with long lifetime.

Because the number of accessible objects alive in the new generation is small, and the number of unreachable objects in the old generation is small, the combination of these two recycling algorithms is very efficient.

3.1 Generational garbage collection

In V8, all JavaScript objects are allocated through the heap. V8 divides the heap it manages into two generations: the new generation and the old generation. The Cenozoic can be subdivided into Nursery and Intermediate generations.

That is, objects in the new generation are objects with a short lifetime, and objects in the old generation are objects with a long lifetime or resident memory.

3.2 Mark-Compact Algorithm (Major GC)

The Mark-Compact algorithm can be seen as a combination of the Mark-sweep algorithm and the Cheney replication algorithm. The algorithm is divided into three stages: marking, clearing and sorting.

image

  1. Mark

    Tagging is the process of finding all accessible objects. GC recursively marks accessible objects from a set of known Pointers to objects, called the root set, including execution stacks, global objects, and so on.

  2. To Sweep

    Cleanup is the process of adding memory space left by unreachable objects to the free list. When memory is allocated for new objects in the future, it can be reallocated from the free linked list.

  3. Compact

    Collation is the process of moving accessible objects to one side of memory. It mainly solves the problem that garbage collection can be triggered in advance when there are many memory fragments in memory space after the mark clearing stage.

3.3 Scavenger Algorithm (Minor GC)

V8 adopts the Scavenger algorithm for the new generation of memory space, which uses a semi-space design: the heap is split in half and only half of the space is always used: from-space is used space and to-space is free space.

The new generation allocates objects in from-space; In the garbage collection phase, the accessible objects in from-space are checked and copied as needed To the to-space or old generation, and the memory Space occupied by unreachable objects in from-space is freed. Finally from-space and to-space swap roles.

The resources

  1. Memory Management
  2. Trash talk: the Orinoco garbage collector