Memory management

  • Memory: Composed of read and write units, representing a piece of operable space
  • Management: the artificial manipulation of the application, use and release of a space
  • Memory management: Developers actively claim space, use space and release space
  • Management process: apply – use – release

Garbage collection in javascript

  • Garbage collection in javascript
  1. Memory management is automatic in javascript;
  2. Objects are garbage when they are no longer referenced
  3. Objects that cannot be accessed from the root are garbage
  • Reachable objects in javascript
  1. Accessible objects are reachable objects (references, scope chains)
  2. The reachable criterion is whether a prescription can be found from the root
  3. Roots in javascript can be thought of as global variable objects

Introduction to GC algorithm

  • GC definitions and functions
  1. GC is short for garbage collection mechanism
  2. The GC finds garbage in memory and frees and reclaims space
  • What is the garbage in the GC?
  1. Objects that are no longer needed in a program
  2. An object in a program that can no longer be accessed
  • What is the GC algorithm?
  1. GC is a mechanism where the garbage collector does the specific work
  2. The content of the job is to find garbage release space, recycling space
  3. An algorithm is a set of rules that lookup and recycle follow at work
  • Common GC algorithms
  1. Reference counting
  2. Mark clear
  3. Tag to sort out
  4. Generational recycling

Implementation principle of reference counting algorithm

  • Core idea: Set the number of references to determine whether the current number of references is 0;
  • Set the reference counter;
  • Modify the reference array when the reference relationship changes;
  • Reclaim immediately if the reference number is 0

Advantages of reference counting algorithms:

  • Recycle rubbish as soon as it is found
  • Minimize program pauses

The reference counting algorithm determines:

  • Unable to recycle objects referenced by loop;
  • Time consuming
Function fn(){let obj1 = {}; let obj2 = {}; obj1.name = obj2; obj2.name = obj1; } fn(); Obj1 and obj2 are cyclic references. The reference count cannot be recycledCopy the code

The realization principle of tag clearing algorithm

  • Core idea: mark and clear two stages to complete
  • Walk through all the objects looking for the mark active object
  • Iterate over all objects to remove unmarked objects
  • Reclaim space

  • Advantages: It can solve the problem of circular reference in reference clearing
  • Confirm: the fragmentation of space will occur, the space cleared is not continuous, and the maximum use of space cannot be achieved

Implementation principle of tag collation algorithm

  • Tag cleanup can be seen as an enhancement to tag cleanup
  • The operation of the tag phase is the same as that of tag clearing
  • The cleanup phase starts with a cleanup, moving the object

Know the V8

  • V8 is a mainstream javascript execution engine
  • V8 uses just-in-time compilation
  • V8 memory limit: 1.5 GB for the 64-bit operating system and 800 MB for the 32-bit operating system, which facilitates garbage collection and reduces the garbage collection time. In the browser environment, the memory is sufficient

V8 garbage collection strategy

  • Adopt the idea of generational recycling
  • Memory is divided into new generation and old generation
  • Different algorithms are used for different objects

Common GC algorithms in V8:

  • Generational recycling
  • Space to copy
  • Mark clear
  • Mark sweep
  • Mark the incremental

How does V8 recycle new generation objects

V8 memory allocation:

  • V8 memory space is split in two
  • A small space is used to store new-generation objects. The space is 16 MB for a 32-bit operating system and 32 MB for a 64-bit operating system
  • Cenozoic refers to objects that have a short life span

New generation object recovery:

  • The recycling process adopts replication algorithm + label finishing
  • The new generation memory is divided into two equally large Spaces
  • The used space is from, and the free space is to
  • Live objects are stored in the FROM space
  • Copy the live object to to after the tag is collated
  • The from and TO interaction space is released

Recycling details:

  • Promotions may occur during copying
  • Promotion is the movement of the new generation to the old generation
  • New generation that survives a GC round needs to be promoted
  • To space usage exceeds 25%, recycling begins

How does V8 recycle old generation objects

Old generation object description:

  • Old generation objects are stored in the old generation area on the right
  • The gb capacity is 1.4 GB for the 64 OS, and 700 GB for the 32 OS
  • An old generation object is an object that lives for a long time

Old generation object recycling implementation:

  • It mainly adopts mark clearing, mark finishing and increment mark algorithm
  • Garbage space is first collected using a tag sweep
  • Spatial optimization was carried out by label finishing
  • Incremental marking is used for efficiency optimization

Detail comparison:

  • New generation of regional garbage recycling use space for time
  • Old-generation area garbage collection is not suitable for replication algorithms

V8 summary

  • V8 is a mainstream javascript execution engine
  • V8 memory upper limit
  • V8 adopts the idea of generational recycling to realize garbage collection
  • V8 memory is divided into new generation and old generation

Code optimization

  • Memory management in javascript is done automatically
  • The execution engine uses different GC algorithms
  • Javascript is an interpreted language with a single-threaded mechanism

Avoid global variables:

  1. Characteristics of global variables
  • Global variables are mounted under the window
  • Global variables have at least one reference count
  • Global variables live longer and continue to consume memory
  1. Why be careful with global variables
  • Global variables are defined in the global execution context and are at the top of any scope chain
  • The global execution context remains in the context execution stack until the program exits
  • The presence of a variable with the same name in a local scope obscures or contaminates the whole world
  1. Avoid global lookup
  • The target variable does not exist in the current scope, as = looks up by scope
  • Reduce global lookup and reduce time consumption
  • Reduce unnecessary global variable definitions
  • Global variable data localization
  1. Caching global variables
  • Cache local global variables that cannot be avoided in use;
  • By using the prototype add method, you can add the methods required by the instance object to the prototype object

Avoid closure traps

  1. Closure features
  • The outsourcing scope can access internal references
  • Access data from the inner scope in the outer scope
  1. About the closure
  • Closures are a powerful syntax
  • Closures are prone to memory leaks when used improperly
  • Don’t close for closure’s sake

Avoid using attribute access methods

  • Js does not require access methods for properties; all properties are externally visible
  • Using attribute access methods only adds another layer of redefinition without access control

Other optimizations:

  • The optimal cycle is adopted
  • For loop optimization, using variables to store array lengths
  • Node addition optimization, node addition operation will inevitably have backflow and redraw, use string splicing DOM, batch addition, avoid multiple addition
  • Clone optimized node operation
  • Replace the Object operation with a literal