GC — understanding of garbage collection mechanism and detailed explanation of algorithm

Time:2021-10-24

catalogue

  • GC related concepts
  • Common GC algorithms
  • Reference counting algorithm

    • Core idea
    • Implementation principle
    • example
    • Advantages and disadvantages
  • Mark removal algorithm

    • Core idea
    • Implementation principle
    • Illustration
    • Advantages and disadvantages
  • Label sorting algorithm

    • Core idea
    • Implementation principle
    • Illustration
    • Advantages and disadvantages

GC related concepts

  • [x] GC: short for garbage collection mechanism. The garbage collection period completes specific work. You can find garbage in memory and free and recycle space
  • [x] GC garbage: in programNo longer neededNo longer accessible in the programobject
  • [x] GC algorithm: it is the rule that GC searches and reclaims when it works

Common GC algorithms

  • Reference count
  • Mark clear
  • Marking arrangement
  • To be recycled separately (for V8)

Reference counting algorithm

Core idea

Set the number of references to determine whether the current number of references is0To determine whether it is a garbage object, if so0JustGCWork and recycle.

Implementation principle

  • Reference counter
  • Modify the reference number when the reference relationship changes
  • The reference number is0Recycle immediately when

example

const user1 = { age: 11 }
const user2 = { age: 12 }
const user3 = { age: 13 }
const nameList = [user1.age, user2.age, user3.age]

function fn() {
  const num1 = 1
  const num2 = 2
  num3 = 3
}

fn()

When the function is called,num1andnum2Cannot be used externally. The number of references is0, will be recycled;
num3Is mounted onwindowOn, so it will not be recycled;
aboveuser1user2user3covernameListReference, so the number of references is not0Will not be recycled;

Advantages and disadvantages

Reference counting algorithm content
advantage 1. Recycle garbage immediately when it is found < br / > 2. Minimize program pause so that the space will not be occupied
shortcoming 1. Unable to recycle circular referenced objects < br / > 2. High resource consumption (monitoring and modifying the values of all objects will take up time and resources)

The following is an example to illustrate that the recycling application object cannot be recycled in the above disadvantages:

function fn() {
  const obj1 = {}
  const obj2 = {}
  obj1.name = obj2
  obj2.name = obj1
  return 'hello world'
}

fn()
//Obj1 and obj2 are referenced to each other, so the counter is not 0. After FN is called, the two objects cannot be recycled

Mark removal algorithm

Compared with the principle, it is simpler to realize and can solve corresponding problems,V8It will be widely used.

Core idea

branchsignandeliminateTwo phases are completed

Implementation principle

  • Stage 1: traverse all objects to find active objects (reachable objects)sign(hierarchies operate recursively)
  • Phase 2: traversing multiple objectseliminateThe object is not marked and the mark of the first stage is erased
  • Reclaim the corresponding space and add the recovered space toFree linked listIt is convenient for the following procedures to apply for space

Illustration

GC -- understanding of garbage collection mechanism and detailed explanation of algorithm

Advantages and disadvantages

Mark removal algorithm content
advantage Compared with the reference counting algorithm, to solve the problem of object circular reference, the content in the local scope cannot be marked, so even if there is a reference, it will be cleared
shortcoming 1. The address of the space linked list is discontinuous (space fragmentation) and cannot be used to maximize space < br / > 2. The garbage object will not be recycled immediately, and the program will stop working when it is cleared.

The following is an illustration of discontinuous addresses in the spatial linked list, which can better help us understand:

GC -- understanding of garbage collection mechanism and detailed explanation of algorithm

The left is released2The space of two words is released in the back1The space of two words, although it seems to be released3A space of words, but the address is discontinuous. If you want to apply for one1.5The space of words is wasted by using the space on the left0.5At the same time, the right side is not enough, which will make it impossible to maximize the use.

Label sorting algorithm

This algorithm is combined with the mark removal algorithmV8It is also widely used in.

Core idea

staysignandeliminateIn the middle, the memory space is addedarrangement

Implementation principle

  • Tag collation can be regarded as the of tag removalenhance
  • Marking stage: consistent with mark clearing
  • Sorting stage: after clearing the money, carry out sorting first, move the location of the object, and generate continuous data on the address
  • Clearing stage: consistent with mark clearing

Illustration

At the beginning, there will be many active and inactive objects and some free space. Start sorting before recycling
GC -- understanding of garbage collection mechanism and detailed explanation of algorithm

After finishing, the inactive objects should be cleared

GC -- understanding of garbage collection mechanism and detailed explanation of algorithm

Finally, the whole free space is reserved

GC -- understanding of garbage collection mechanism and detailed explanation of algorithm

Advantages and disadvantages

Label sorting algorithm content
advantage Compared with the mark removal algorithm, the fragmentation space is reduced
shortcoming The garbage object will not be recycled immediately. The program stops working when it is cleared.