Garbage collection: JS is a language that uses garbage collection to allocate memory and recycle idle resources through automatic memory management.

The basic idea: Determine which variable is no longer used, and then release the memory it occupies. This process is periodic, running automatically at regular intervals, and the developer does not have to worry about memory allocation and reclamation.

  1. Values that leave scope are automatically marked as recyclable and then deleted during garbage collection
  2. The dominant garbage collection algorithm is tag cleaning, which marks a value that is not currently in use and then comes back to reclaim its memory
  3. Reference counting is another way to keep track of how many times a value is referenced.

What is junk data

When we write JS code, we manipulate data frequently. When some data is not needed, it is junk data. The memory occupied by these garbage data should be reclaimed.

const dog = new Object(a)

dog.a = new Array(a)

Copy the code

When JS executes the code, it first adds a dog property to the global scope and creates an empty object in the heap with its address pointing to dog. We then create an array of size 1 and point the property address to dog.a


If we set dog.a = new Object()


When the a pointer changes, the array object in the heap becomes junk data, the technical term “unreachable” data, and needs to be recycled.

Garbage collection algorithm

Think of the process as a giant bucket of paint spilling from the root, starting from a root node, staining reachable objects with markers and removing the unlabeled ones.

  1. Mark the value of reachable value in the space

    V8 uses the reachability algorithm to determine whether objects in the heap should be recycled or not. Ideas:

    1. Starting from the root node, all objects are traversed
    2. Objects that can be traversed are reachable
    3. What is not traversed is unreachable
  2. Reclaim the memory occupied by unreachable values

    After marking, all unreachable objects in memory are cleaned uniformly

  3. Memory consolidation

    1. When objects are recycled frequently, there is a large amount of discontinuous space in memory.
    2. When there is a large amount of memory fragmentation in memory, it is possible to run out of memory if large contiguous memory needs to be allocated.
    3. The last step is defragmentation

When is the garbage collected

When the browser garbage collection, it will suspend the JS script, and wait for the completion of the collection to continue for ordinary applications no problem, but for JS games, animation requires high consistency of the application, resulting in page lag. When to recycle garbage to avoid long pauses.

  • Generational collection
  • Incremental collection
  • Spare time to collect

Generational collection

Browsers divide data into two types:

  • Temporary objects

    • Most objects have a short lifetime in memory.
    • A variable declared inside a function that is destroyed after the function is executed.
    • Variable is not accessible and should be reclaimed quickly.
  • The object for a long time

    • Long-life objects, global Windows,DOM,WEBAPI, etc.
    • Such objects can be recycled more slowly.

For these two types of objects, V8 divides the heap into:

  1. New generation area (storing temporary objects) == “secondary garbage collector collection
  2. The old generation area (holding persistent objects) == is collected by the main garbage collector

Main garbage collector

Responsible for long-term objects in old age areas, characteristics:

  • Take up large space
  • The object has a long lifetime

It uses a mark-sweep algorithm to perform garbage collection

  1. Start with a set of root elements and recursively traverse the set of root elements. The elements that can be reached are called active objects. The elements that can’t be reached can be judged as junk data.
  2. Garbage removal
Garbage data is removed directly
  1. Memory defragmentation (A large number of discontinuous memory fragments will be generated after multiple cleaning, which requires memory defragmentation)
Tag to sort out

Secondary garbage collector

Responsible for the collection of temporary objects in the new generation area, usually only 1-8m capacity. The Cenozoic era is divided into two regions:

  • New area
  • The free zone

New objects are put into the object area, and when the object area is full, a garbage collection is performed.

Steps:

  1. Mark all garbage in the object area first
  2. After marking, the surviving objects are copied into the free area and arranged in order

The paralgarbage collector does not defragment because the free area is inherently neat.

  1. The free area is switched with the object area

Summary: The by-garbage collection operation is relatively frequent. In order to implement efficiency, the newly collected space is generally set to be small, and the garbage collection will be carried out once it is full.

Generation collection: the heap is divided into new generation and old generation, and more new generation and less old generation are recycled. This reduces the number of objects that need to be traversed each time, thus reducing garbage collection time.


Incremental collection

If there are many objects in the script, the engine will traverse the entire object at once, causing long pauses. So the engine breaks up garbage collection into smaller chunks, one part at a time, several times. This solves the problem of long pauses.


Spare time to collect

The garbage collector only tries to run when the CPU is idle to reduce the possible impact on code execution.

The interview questions

1. How do browsers garbage collect?

Three answers: What is rubbish, how to pick it up and when to pick it up

What is garbage: Data that is no longer needed is garbage, and global variables are always available, not garbage

How to pick up litter :(traversal algorithm)

  • Reachable values in the marker space
  • Reclaim memory occupied by unreachable values
  • Do memory defragment

When do you pick up litter

  • The front end is special and garbage collection may cause page lag
  • Generation collection, incremental collection, idle collection