Summary as# # #

  1. Memory management
  2. Garbage collection and common GC algorithms
  3. V8 engine garbage collection
  4. The performance tools
  5. Code optimization examples

JavaScript Memory Management

Introduction to Memory Management

  • Memory: Composed of read and write units, representing a piece of operable control
  • Management: the artificial manipulation of the application, use and release of a space
  • Memory management: developers actively apply for controls, use controls, release controls
  • Management process: Apply -> Use -> Release

Memory management for javaScript

  • Applying for Memory Space
  • Using memory controls
  • Free memory control
/ / application
let obj = {}

/ / use
obj.name = 'lg'

/ / release
obj = null
Copy the code

JavaScirpt garbage collection

  • Memory management is automatic in javaScript
  • Objects are garbage when they are no longer referenced
  • Objects that cannot be accessed from the root are garbage

Reachable objects in javaScript

  • Accessible objects are reachable objects (references, scope chains)
  • The criterion of reachability is whether it can be found from the root
  • Roots in javaScript can be thought of as global variable objects

References and reachability in javaScript

GC algorithm

GC definitions and functions

  • GC is short for garbage collection mechanism
  • The GC finds garbage in memory and frees and reclaims space

What is the garbage in the GC

  • An object that is not needed in a program

  • An object in a program that can no longer be accessed

What is the GC algorithm

  • GC is a mechanism where the garbage collector does the specific work
  • The content of the job is to find the garbage release control, recycling control
  • An algorithm is a set of rules that lookup and recycle follow at work

Common GC algorithms

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

Implementation principle of reference counting algorithm

Reference counting algorithm

  • Core idea: Set the number of references to determine whether the current number of references is 0
  • Reference counter
  • Modify the reference number when the reference relationship changes
  • A reference number of 0 is immediate collection

Advantages of reference counting algorithm

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

Disadvantages of the reference counting algorithm

  • Object referenced by loop cannot be recycled
  • Time consuming

The realization principle of tag clearing algorithm

Mark 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

Summary of common GC algorithms

V8 engine

  • V8 is a mainstream JavaScript execution engine
  • V8 uses just-in-time compilation
  • V8 memory limit

V8 Memory Allocation

  • V8 memory controls are split in two
  • Little space to store the new generation object (32 m | 16 m)
  • Cenozoic objects are objects that have a short lifespan

New generation object recovery implementation

  • The recycling process adopts replication algorithm + label finishing
  • The new generation of memory is divided into two equal size 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 exchange space between From and To is released completely

Why use Performance

  • The purpose of GC is to achieve a virtuous cycle of memory space
  • The cornerstone of a virtuous cycle is rational use
  • Keep an eye on it to see if it makes sense
  • Performance Provides multiple monitoring modes

Performance Procedure

  • Open your browser and enter the destination url
  • Go to the Development tool panel and select Performance
  • Enable the recording function to access the specific screen
  • Perform user action and stop recording after a certain period of time
  • The results of the analysis

External manifestation of memory problems

  • Pages load lazily or pause frequently
  • The page continues to have poor performance
  • Page performance deteriorates over time

Several ways to monitor memory

Criteria for defining memory problems

  • Memory leaks, memory usage continues to rise
  • Memory bloat and performance issues on most devices
  • Frequent garbage collection: Analyzed by memory variation graph

Several ways to monitor memory

  • Task manager for the browser
  • Timeline Time sequence chart records
  • The heap looks for separate DOM
  • Determine if there is frequent garbage collection

The task manager monitors memory