preface

Unlike C/C++, which allows programmers to create or free memory, JS is similar to Java, which uses its own set of garbage collection algorithms for automatic memory management.

We know that for stack memory, when the ESP pointer moves down, i.e. after a context switch, the space at the top of the stack is automatically reclaimed. However, heap memory is more complicated, and we will focus on the garbage collection of heap memory.

V8 Memory limits

In other backend languages, such as Java/Go, there are no limitations on memory usage, but unlike JS, V8 can only use a portion of the system’s memory. Specifically, V8 can only allocate a maximum of 1.4 GB on 64-bit systems and 0.7 GB on 32-bit systems.

Cause of V8 memory limitation

  • JS is single-threaded, which means that once garbage collection is in place, all other running logic is suspended
  • Garbage collection is a very time consuming operation

V8 memory limit adjustment

// The new generation unit is MB node --max-old-space-size=2048 xxx.js // the new generation unit is KB node --max-new-space-size=2048 xxx.jsCopy the code

V8 Memory Reclamation

V8 splits the heap memory into two parts for processing – the new generation and the old generation. As the name implies, the new generation is temporarily allocated memory, which lives for a short time, while the old generation is resident memory, which lives for a long time. V8 heap memory, which is the sum of two memories.

Based on these two different types of heap memory, V8 uses different reclamation strategies that are optimized for different scenarios.

New generation memory reclamation

First, the new generation of memory space is divided into two parts, namely, From and To, where the From part represents the memory in use, To is the current idle memory.

When garbage collection is performed, V8 checks the objects in the From section and copies them To To memory if they are alive (they were placed From scratch in order in To memory) or directly collects non-alive objects.

After all the surviving objects in the From are in the To memory in sequence, the roles of the From and To are reversed, the From is now idle, the To is in use, and so on.

The garbage recycling process described above is also called the Scavenge algorithm.

In order from the beginning in To memory?

In order from the beginning in To memory, this is To cope with such a scenario.

When there is a lot of unallocated object space scattered in memory, there may be no way to allocate space for the slightly larger objects. This kind of scattered space is also called memory fragmentation.

To solve this problem, the Scavenge algorithm allocates objects neatly into contiguous memory space.

The advantages and disadvantages of the Scavenge algorithm

Memory is only half of the memory of the new generation, but it only holds objects with short life cycles, which are generally few, so the time performance is very good.

Old generation memory reclamation

If the variables in the new generation still exist after many recycling, they will be put into the old generation memory, which is called promotion.

Promotions occur when you have already been recycled, or when the amount of memory used exceeds 25%.

So for old generation, what kind of strategy is adopted for garbage recycling?

Please read the previous article “Garbage Collection in Seconds with JS”.

Thanks for reading!

Need to add wechat communication, can leave a message!