Note: This article focuses on language optimization for JavaScript itself, not optimization for our code.

1. JavaScript memory management

  • What is memory

Memory, also known as internal Memory and main Memory, is used to temporarily store computational data in the CPU and data exchanged with external Memory such as hard disk. All programs in a computer are run in memory, so the performance of memory has a great impact on the computer.

  • Memory life cycle

    1. Allocate as much memory as you need
    2. Use allocated memory (read, write)
    3. Release \ return it when it is not needed
  • Memory usage

The process of working with values is actually reading and writing to allocated memory. Reading and writing may be writing the value of a variable or an object property, or even passing the parameters of a function.

  • Memory release

Most memory management problems occur at this stage. The hardest task here is to find out that “the allocated memory is really no longer needed.” It often requires the developer to determine which chunk of memory in the program is no longer needed and to release it.

JavaScript garbage collection

1. What does garbage recycling refer to

In general, objects that are not referenced are garbage and should be removed, with the exception that if several object references form a ring that refers to each other, but the root can’t access them, those objects are also garbage and should be removed.

2. The necessity of garbage recycling

Because strings, objects, and arrays have no fixed sizes, they can only be allocated dynamically when their sizes are known. Every time a JavaScript program creates a string, array, or object, the interpreter must allocate memory to store that entity. Whenever memory is allocated dynamically like this, it must eventually be freed so that it can be reused, otherwise the JavaScript interpreter will consume all available memory in the system, causing the system to crash.

3. How does JavaScript do garbage collection

To understand how JavaScript does garbage collection, we need to look at JavaScript’s GC algorithm.

GC algorithm in JavaScript

1. Definition and function of GC algorithm

The GC is a mechanism where the garbage collector does the specific work, and the algorithm is the rules that are followed for lookup and collection at work.

The content of the work is to find the garbage release space, recycling space.

2. Common GC algorithms

  • Reference counting

Mechanism: Add a reference number inside the object and check whether the reference number is 0. When the reference relationship changes, you can modify the number of references. If the number of references is 0, it is reclaimed immediately.

Advantages:

1. Recycle garbage immediately (monitor the number of references).

2. Minimize program pauses (garbage can be collected immediately).

Disadvantages:

1. The reference to the loop object cannot be reclaimed.

2. High time consumption (constantly monitoring the number of references).

  • Mark clear

Mechanism: Iterate over all objects and mark active objects (reachable objects), then iterate over all objects again while clearing unmarked objects and removing marks from marked objects. Finally, the space is reclaimed.

Advantages:

1. References to loop objects can be reclaimed.

Disadvantages:

1. Spatial fragmentation: When A string of addresses stores three variables A, B and C, A and C are clear. So you’re left with A and C with A fixed amount of space. The length of the variable that is added next is unknown. It’s hard to be exactly the same length as A and C, so the utilization of space is definitely going to go down.

  • Tag to sort out

Mechanism: Tag cleanup can be seen as an enhancement to tag cleanup, which, like tag cleanup, iterates through all objects and marks all active objects. The unmarked variables are then cleared. Just move the variable to be cleared before clearing to free up contiguous space. Thus avoiding the disadvantage of tag clearing. Advantages:

1. Reduce defragmentation.

Disadvantages:

1. Garbage objects are not immediately collected. 2. The reclaiming efficiency is slow because the object is moved.

Get to know V8

1. What is V8

  • V8 is a mainstream JS execution engine.
  • V8 is fast because of just-in-time compilation.
  • V8 has memory limits of up to 800MB for 32-bit and 1.5GB for 64-bit.
  • Data of value types is stored in the stack and collected by the system. Generally, garbage collection is the collection of complex data types in the heap.

2. V8 garbage collection strategy

  • V8 uses the idea of generational recycling.
  • Memory is divided into new generation storage area, old generation storage area.
  • In order to distinguish the new generation from the old generation, the V8 memory space is divided into two parts: the left side stores the new generation and the right side stores the old generation.
  • Different algorithms are used for different kinds of objects to achieve the most efficient processing.

3. GC algorithm commonly used by V8

  • Recycle the new generation and old generation by generation
  • Space replication From space To To
  • Mark clear
  • Tag to sort out
  • Mark the incremental

4. Garbage collection of new generation objects

  • Small space for storing new generation objects. The maximum size is 32MB for 64-bit objects and 16MB for 32-bit objects.
  • Cenozoic objects are objects that have a short lifetime, such as variables in the local scope of a function.
  • The recycling process adopts replication algorithm + label finishing.
    • Divide the new generation memory into two equally sized Spaces.
    • The used space is From, and the free space is To.
    • Live objects are stored in the From space where To is always free.
    • The GC operation is triggered when the From space is applied to a certain extent.
    • The live object is copied into the To space after the tag is collated.
    • The From space is completely freed and then swapped with the To space completes the garbage collection.
  • New generation object recovery details:
    • Promotions are possible during copying.
    • Promotion: Refers to the movement of new generation objects to old generation for storage.
    • Generally new generation objects that survive a round of GC need to be promoted.
    • These objects also need To be promoted if the usage of the To space exceeds 25%.
    • Because if the “To” space is used too much when “To” becomes “From”, there might not be much memory left for the data that comes in.

5. Old generation object garbage collection

  • Large space is used to store old generation objects. The maximum 64-bit size is 1.4GB, and the maximum 32-bit size is 700MB.
  • A legacy object is an object that has a long lifetime, such as a global variable or a variable in a closure
  • The recycling process adopts the algorithm of tag clearing + tag finishing + tag increment.
    • First of all, the garbage space is collected using the tag sweep.
    • When a new generation is promoted and the old generation memory block is insufficient to store the promoted data, the marker collation is triggered to defragment the memory.
    • Incremental marking is used for efficiency optimization.
    • Incremental tagging: Tagging blocks code execution, so the entire tagging process is broken up into a number of alternating inserts to mark during execution.

How to optimize the code level

How do we do optimization at the code level? Here I recommend an article: JS performance optimization Strategy.