First, memory management

  • Memory: The memory in which all programs in a computer run. It consists of read/write units and represents a piece of operable space
  • Management: the artificial manipulation of the application, use and release of a space
  • Memory management: developers actively apply for space, use space, free space
  • Management process: Apply -> Use -> Release

JavaScript does not support developers to manually manage memory. When the JS execution engine executes code, it automatically allocates space in memory for variables

// Apply for memory space
let obj = {}

// Use memory space
obj.name = 'reborn'

// Free up memory
obj = null 
Copy the code

Garbage collection in JavaScript

Definition of garbage

  • Variables in a program that are not referenced (not needed) are garbage
  • Programs that can no longer be accessed are garbage

When these two points are met, the JS execution engine will release and reclaim the garbage (space) in memory.

Reachable objects: From the root (global scope), accessible objects are reachable objects (scope, reference)

// The obj variable references the memory space
const obj = {name: 'rebornjiang'}

// Assign a reference to obj to the newObj variable. Currently, newObj also references this memory space.
const newObj = obj

// Free the obj reference
obj = null

// {name: 'rebornjiang'} this memory space is still reachable, and there is still space to be referenced, so it cannot be freed.
Copy the code

GC algorithm

Introduction: GC is short for garbage collection: the GC can find garbage in memory and free and reclaim space.

Common GC algorithms

  • Reference counting
  • Mark clear
  • Tag to sort out
  • Generational recycling

1. Reference counting algorithm

Core ideas:

  • There are special reference counters to count the number of references to an object (variable)
  • The reference number is modified when the reference relationship changes
  • Determines if the current reference count is 0, and if it is 0, the GC releases and collects it as garbage.

Code demo

// case 1
const user1 = { age: 18 }
const user2 = { age: 20 }
const user3 = { age: 21 }

const ageList = [user1.age, user2.age, user3.age]

The ageList array contains references to user1, user2, user3
// The reference count for these objects is not zero, and the GC will not release or reclaim them.


// case 2
function fn() {
  const num1 = 1
  const num2 = 2
}

fn()

// When fn() finishes, num1 and num2 are not in use, and their reference counter is 0, GC will reclaim them.

Copy the code

Advantages:

  • It is found that it can be reclaimed immediately, and it can be freed immediately when the reference to the space in memory monitored is 0.
  • This minimizes the possibility of program pauses, because the reference counting GC will always listen for references to objects in memory space that are 0, and if 0 is immediately released, thus minimizing the possibility of program pauses

Disadvantages:

  • Unable to recycle an object referenced by a loop. Refer to the following code example
  • Compared with other GC algorithms, the time cost is large. This is because the reference counting algorithm needs to maintain a reference count on the memory space object, listen for changes in the reference, and when the reference changes, the reference count changes accordingly. However, there are many spatial objects in memory that need to be maintained and listened to for reference counting, which can cause the time overhead of reference counting algorithm.
// A circular reference to an object
function fn() {
  const obj1 = { name: 'rebornjiang' }
  const obj2 = { name: 'molly' }

  obj1.name = obj2
  obj2.name = obj1
  return ' '
}
fn()

Obj1 and obj2 are not referenced globally, but at this point the name attribute of obj1 and obj2 refers to each other
// Its reference count is not zero, so it cannot be released and collected by reference counting garbage collection.
Copy the code

2. Mark clearing algorithm

The core idea of the tag clearing algorithm is as follows:

  • There are two stages, marking and clearing
  • All objects are iterated over, and their active objects (reachable objects) are marked
  • Iterate through all the objects, and then clear the non-marked objects, release the corresponding reclaimed space, and add the reclaimed space to the free linked list, waiting for application for use.
  • Untag the tag object at the same time.

advantages

  • Relative to the reference counting algorithm it can clear the circular reference object

// Convert the above illustration to code
const A = {
  D: {}}const B = {}

const C = {
  E: {}}function fn() {
  const a1 = {}
  const b1 = {}
  a1.obj1 = b1
  b1.obj2 = a1

  return ' '
}

fn()

// When fn is executed, a1 and b1 have no relation to the global object, a1 and b1 are programed as unreachable objects.
// In the first phase of the tag removal algorithm, a1 b1 is not marked, waiting for the second tag removal algorithm
// Phase will clear a1 b1 and reclaim space, add the reclaimed space to the free linked list, and wait for application.
Copy the code

disadvantages

  • Reclaimed space will be fragmented and will not be able to maximize the utilization of space.
  • Marked cleanup does not immediately remove garbage
    • The program pauses while the tag cleanup and tag cleanup GC is running

See analog memory storage

Any memory space object will have two parts: header + field header: storage space source information, such as space size, space address information. Domain: Used specifically to store data Illustration of example diagram:

  • The mark clearing algorithm marks A as reachable in the first marking phase, while BC is not marked.
  • The second phase of the token-clearing algorithm will clean up and reclaim the space of untagged BC unreachable objects and add them to the free linked list, waiting to be used.
  • B space objects will have two domain storage Spaces and C space objects will have one domain storage space. B and C together seem to free three domain storage space, but because there is an extra A space in B and C memory space, this causes the space address of reclaimed space BC to be discontinuous, which causes the fragmentation of memory space.
  • The fragmented space can not be used in combination, which results in the maximum utilization of the space required to be matched with the size of the released space.

3. Realization principle of tag collation algorithm

Core ideas:

  • The label cleaning algorithm is an enhanced operation of the label cleaning algorithm to solve the problem of space fragmentation caused by the label cleaning algorithm.
  • The operation of the tag phase is the same as that of the tag cleaning algorithm GC
  • Mark collation is performed prior to the cleanup phase to separate marked reachable objects from unmarked unreachable objects, resulting in contiguous spatial addresses to be reclaimed.

Please check the following figure: advantages

  • It has the same advantages as tag clearance
  • It makes up for the disadvantage of space fragmentation caused by mark clearing

disadvantages

  • The program pauses while the tag cleanup and tag cleanup GC is running
  • Garbage cannot be collected immediately

4. Generational recycling algorithm

The V8 garbage collection strategy is generational collection, see the next section.

Iv. Understanding of V8 JS execution engine

The understanding of the V8

  • V8 is the JS execution engine
  • V8 is fast because of just-in-time compilation. Unlike other JS execution engines that need to compile code into bytecode and interpret it into machine code. The V8 engine can translate JS code directly into machine code for execution.
  • V8 memory has an upper limit.

V8 garbage collection strategy adopts the idea of generational collection, which is divided into new generation objects and old generation objects. Different GC algorithms are adopted for garbage collection of objects of different generations.

  • The V8 executive engine divides the memory storage space into the new generation storage space and the old generation storage space
  • Different GC algorithms are used for different generations of objects to achieve the highest garbage collection efficiency.

The concept of new generation old generation object

  • New generation objects: Objects that have a short lifetime, such as members of a local scope within a function, are recycled after execution.
  • Legacy objects: long-lived objects, such as members of a global scope, closure members

V8 common GC algorithm

  • Generational recycling
  • Space to copy
  • Mark clear
  • Tag to sort out
  • Mark the incremental

1. V8 Recycling new generation objects

Objects of the new generation are stored in the new generation storage space. The storage space varies according to operating systems (oss). The storage space is 32 MB on OSx64 and 16 MB on OSx32.

The principle of new generation object recovery

  • The new generation object is garbage collected by copying algorithm + tag sorting algorithm
  • Divide the new generation memory space into two Spaces of equal size, as shown in the From and To modules above
  • The used space is From, and the free space is To
  • All live objects will be allocated to the From space
  • When the size of the From space reaches a certain point, the tokenized GC operation is triggered To copy the tokenized live objects into the free space To
  • From space and To space complete a role reversal, To space becomes the new From space, the original From space becomes To space.

Recycling details:

  • Promotions may occur during copying
  • Promotion is the movement of the new generation to the old generation
  • Promotion depends on two criteria
  • A new generation of inventory collectors needs to be promoted
  • To space usage exceeds 25%

2. V8 reclaims the old generation object

The old generation objects are stored in the old generation storage space. The size of storage space varies according to the operating system. The size of storage space is 1.4 GB on OSx64 and 700 MB on OSx32.

Old generation object recycling implementation principle

  • It mainly adopts mark clearing, mark finishing and increment mark algorithm
  • Garbage space is first collected using a tag sweep. The fragmented storage space is processed by token collation algorithm.
  • When the new generation objects want to move to the old generation objects, and the memory space of the old generation objects is not enough to allocate to the moved objects, the mark collation is used to optimize the fragmented space.
  • Incremental marking is used for efficiency optimization

Incremental marking algorithmAs can be seen from the figure, the incremental marking algorithm does not mark the reachable object at one time, but is segmented to provide the efficiency of garbage collection.