Learn 8. JavaScript memory management and garbage collection (GC algorithm)

One, memory management

    1) Memory: composed of read and write units, representing a piece of operational space

    2) Management: artificially operate the application, use and release of a space

    3) Memory management: developers actively apply for space, use space, and release space

    4) Management process: application————use————release

 

Second, garbage collection and common GC algorithms

1. Garbage collection in JavaScript

      1) Memory management in JavaScript is automatic

      2) The object is garbage when it is no longer referenced

      3) The object cannot be accessed from the root and is garbage

JavaScript reachable objects: the objects we can access are reachable objects (reference, scope chain)

      ————The reachable criterion is whether it can be found when triggered from the root

The root in JavaScript can be understood as a global variable object

 

2. The definition and role of GC

        1) GC is short for garbage collection mechanism

        2) GC can find garbage in memory, release and reclaim space

            Objects no longer needed in the program, objects that can no longer be accessed in the program

        3) Algorithms are the rules for searching and recycling at work

        (Reference counting, mark removal, mark sorting, generational collection)

 

3. Reference counting algorithm:

        Core idea: Set the number of references, and judge whether the current number of references is 0

        Reference counter

        Modify the reference number when the reference relationship changes

        Reclaim immediately when the reference number is 0

        Advantages: Recycle immediately when garbage is found, minimizing program suspension

        Disadvantages: Unable to recover circularly referenced objects, which consumes a lot of resources

 

4. Mark removal algorithm:

        Core idea: To complete in two stages: mark and clear

        Traverse all objects to find and mark the active object

        Traverse all objects to clear unmarked objects

        Reclaim the corresponding space

        Advantages can find some local scope garbage, which can solve the problem of not recycling circular references in the reference counting algorithm

        Disadvantages: Garbage objects will not be reclaimed immediately, addresses are not continuous, space is fragmented, and space is wasted

 

5. Marking and sorting algorithm:

        Marking can be seen as an enhancement of mark removal

        The operation of the marking phase is the same as that of the marking removal

        In the cleaning phase, it will be sorted first and move the object position

        Advantages Reduced fragmented space

        Disadvantages will not be recycled immediately

 

Third, the garbage collection of the V8 engine

    1) V8 is a mainstream JavaScript execution engine

    2) V8 uses just-in-time compilation

    3) V8 memory limit (64-bit 1.5G, 32-bit 800MB)

 

    1. V8 garbage collection strategy

        1) Adopt the idea of ​​generational recycling

        2) The memory is divided into the new generation and the old generation

 

    2. Common GC algorithms in V8

        * Generational recycling

        * Space copy

        * Mark clear

        * Mark up

        * Mark increment

 

    3. How does V8 reclaim new generation objects

        1) V8 memory space is divided into two

        2) Small space is used to store new generation objects (32M | 16M)

        3) The new generation system is a short-lived object

 

        The new generation object recycling implementation:

        1) The recycling process adopts a copy algorithm + tagging

        2) The new generation memory is divided into two equal spaces, the used space is From and the free space is To

        3) Active objects are stored in the From space

        4) Copy the active object to To after marking

        5) Clear the From space to complete the release, and exchange the From and To space

 

    Recycling details

        1) Promotion may occur during the copy process

        2) Promotion is to move the new generation object to the old generation

        3) The new generation that survives a round of GC needs to be promoted

        4) When the usage rate of the To space exceeds 25%, all objects in the To space will be promoted

 

    4. How does V8 reclaim old generation objects?

        1) Old generation objects are stored in the old generation area

        2) Large space is used to store old generation objects (1.4G | 700M)

        3) Old generation objects refer to long-lived objects (global variables, closures)

 

        The old generation object recycling implementation:

        1) Mainly use mark removal, mark sorting, and incremental mark algorithms

        2) First use mark clear to complete the garbage space recycling

        3) Use tag sorting for space optimization (when the young generation is promoted to the old generation, the fragments of the old generation space are not enough to store)

        4) Use incremental marking for efficiency optimization

 

    5. Incremental marking optimizes garbage collection

        When the program is executed, garbage collection is performed alternately, the objects are traversed hierarchically to mark, and finally the removal is completed.

 

Four, performance tools

 

    1. The purpose of GC is to realize the conscience cycle of memory space

    2. The cornerstone of the conscience cycle is rational use

    3. Always pay attention to determine whether it is reasonable

    4. Performance provides multiple monitoring methods

    5. Use steps:

        1) Open the browser and enter the destination URL

        2) Enter developer tools, select performance

        3) Turn on the recording function and access the specific interface

        4) Perform user actions and stop recording after a period of time

        5) Analyze the memory information recorded in the interface

 

Five, how to judge the memory problem

 

    1. The manifestation of memory problems

        1) Delayed loading or frequent pauses in the page: frequent garbage collection, excessive garbage generated by the program code

        2) Poor performance of the page persistence: memory expansion, the current interface will apply for a certain amount of space in order to achieve a certain speed of use, the size of this space far exceeds the size of the space we can provide

        3) The performance of the page gets worse and worse with time: memory leaks, memory that cannot be recycled for some reason.

 

    2. Standards for defining memory problems

        1) Memory leak: memory continues to rise

        2) Memory bloat: There are performance issues on most devices

        3) Frequent garbage collection: analysis through the memory change graph

 

    3. Several ways to monitor memory

        1) Browser task manager

            Open the browser, open the task manager through shift+esc, right-click to open JavaScript to use memory

            If the memory in parentheses keeps increasing without being consumed, there is a memory leak problem

 

        2) Timeline timing chart record

            Open the browser, F12, find Performance, record the screen, monitor the memory trend

 

        3) Heap snapshot search to separate DOM

            What is separate DOM: It is separated from the current DOM tree and is referenced in JS code.

            Open the browser, F12, find Memory, select Profiles=>Heap snapshot=>takeSnapshot=>filter'deta' to get all separated DOM

 

        4) Determine whether there is frequent garbage collection

            When the GC is working, the application will freeze. If the GC is frequent and too long, the user will experience a poor performance experience

            Frequent rise and fall in the Timeline

Guess you like

Origin blog.csdn.net/qq_40289624/article/details/108897534