One, the V8 engine memory limit

In a normal back-end development language, there is no limit to the basic memory usage, whereas when using memory in JavaScript in Node, you will find that you can only use partial memory (about 1.4GB for 64-bit systems and 0.7 GB for 32-bit systems). This limitation prevents Node from directly manipulating large memory objects, such as reading a 2 GB file into memory for string parsing, even though there is 32 GB of physical memory. In the case of a single Node process, the computer’s memory resources are not fully utilized.

In V8, all JavaScript objects are allocated via the heap.

As for why V8 restricts the size of the heap, the superficial reason is that V8 was originally designed for browsers and is unlikely to encounter scenarios that use large amounts of memory. V8’s limit is more than enough for web pages. The underlying reason is the limitations of V8’s garbage collection mechanism. Using 1.5 GB of garbage collection heap memory as an example, V8 takes more than 50 milliseconds to do a small garbage collection and even more than a second to do a non-incremental garbage collection. This is the amount of time in garbage collection that causes JavaScript threads to pause, and the performance and responsiveness of your application can plummet with this amount of time. This situation is unacceptable not only for back-end services, but also for front-end browsers. Therefore, limiting heap memory directly was a good choice for the time being

Second, V8 engine garbage collection mechanism

V8’s garbage collection strategy is based on generational garbage collection. In the evolution of automated garbage collection, it has been found that no single garbage collection algorithm is suitable for all scenarios. Because in practical applications, the life cycle of objects varies, different algorithms can only have the best effect for specific situations. For this reason, statistics plays an important role in the development of garbage collection algorithms. In modern garbage collection algorithms, garbage collection of memory is divided into different generations according to the survival time of objects, and then more efficient algorithms are implemented for different generations of memory.

V8 memory generation In V8, the main memory is divided into the new generation and old generation. Objects in the new generation are objects with a short lifetime, and objects in the old generation are objects with a long lifetime or resident memory. The total size of the V8 heap is the memory used by the new generation plus the memory used by the old generation. The –max-old-space-size command line parameter can be used to set the maximum memory size of the old generation. The –max-new-space-size command line parameter is used to set the memory size of the new generation. Unfortunately, these two maximum values need to be specified at startup. This means that the memory used by V8 has no way to automatically expand based on usage, and when the memory allocation limit is exceeded, the process will fail.

By default, the maximum V8 heap memory is 1464 MB on 64-bit systems and 732 MB on 32-bit systems. This number explains why you can only use about 1.4GB of memory on 64-bit systems and about 0.7GB on 32-bit systems.

Scavenge algorithm for new generation algorithms

The Scavenge algorithm is used to recycle garbage on a generational basis. The Cheney algorithm is used in the application of Scavenge,

Cheney algorithm is a garbage collection algorithm implemented by copying. It divides the heap memory into two parts, each of which is called semispace. Of the two Semispace Spaces, only one is in use and the other is idle. Semispace Spaces that are in use are called From Spaces, and Spaces that are idle are called To Spaces. When we allocate objects, we allocate them first in the From space. When garbage collection begins, live objects in the From space are checked, they are copied To the To space, and space occupied by non-live objects is freed. After the replication is complete, the roles of the From space and To space are swapped. In a nutshell, garbage collection is done by copying live objects between two Semispace Spaces.

The disadvantage of the Scavenge is that only half of the heap memory is used, as determined by the partitioning and replication mechanisms. Scavenge, however, is exceptionally time efficient because it only replicates live objects and is used in a small number of scenarios with a short life cycle. Scavenge is the classic space-for-time algorithm and cannot be applied on a large scale to all garbage recycling. The Scavenge avenge is found to be extremely suitable for use in the Cenozoic era, where the life cycle is shorter.

When an object survives multiple copies, it is considered a long-lived object. Such long-lived objects are then moved to the old generation and managed with a new algorithm. The process of moving objects from the new generation to the old generation is called promotion.

In the pure Scavenge process, surviving objects From the From space are replicated into the To insane, and role-swapping is performed on the From and To. In generational garbage collection, however, living objects in the From space need To be checked before being copied To the To space. Under certain conditions, it is necessary to move objects with a long lifetime into the old generation, that is, to complete object promotion.

To be promoted is based on the Scavenge avenge and the insane memory usage.

By default, V8’s object allocation is concentrated in the From space. When an object is copied From the From space To the To space, its memory location is checked To determine whether the object has been screcycled. If it has, the object is copied From the From space To the old space, if not, To the To space.

Another criterion is the memory footprint of the To space. When copying an object From the From space To the To space, if more than 25% of the To space is already used, the object is promoted directly To the old space.

The reason for the 25% limit is that when the Scavenge is completed, the To space becomes the From space and subsequent memory allocation takes place in that space. If the ratio is too high, subsequent memory allocation will be affected.

After the object is promoted, it will be treated as an object with a long lifetime in the old generation space and be treated by the new collection algorithm.

Old generation recycling algorithm

The Scavenge avenge has two problems due to the large proportion of the exploiture. The first is the low efficiency of copying the exploiture due to the large number of exploiture. The other problem, again, is the waste of half the space. Both of these issues result in the exploiture of applications with a longer life cycle. To this end, V8 used a combination of Mark-sweep and Mark-compact for garbage collection in its old generation

Mark-Sweep

Mark-sweep means marked Sweep and is divided into two phases: marking and sweeping. In contrast to the Scavenge, Mark-Sweep does not split memory space in half, so there is no such thing as wasting half the space. Unlike the Scavenge avenge living objects, Mark-sweep iterates through all objects in the heap and marks living objects during the marking phase, and only unmarked objects are cleared during the subsequent scavenging phase. As you can see, Scavenge only copies living objects and Mark-sweep only cleans dead objects. The living objects only account for a small part in the new generation, and the dead objects only account for a small part in the old generation, which is the reason why the two recycling methods can be processed efficiently.

The biggest problem with Mark-Sweep is the discontinuous state of memory space after a Mark Sweep collection. This fragmentation can cause problems for subsequent memory allocation because it is likely that a large object will need to be allocated, and garbage collection will be triggered ahead of time, which is not necessary. In order to solve mark-Sweep’s memory fragmentation problem, Mark-Compact was proposed. Mark-compact stands for mark-sweep and is a variation of mark-sweep. The difference is that after an object is marked as dead, the living object is moved to one end during the collation process. When the move is complete, the memory outside the boundary is cleared directly.

The combination of Mark-sweep and Mark-compact is introduced here not only because the two strategies are progressive, but in V8’s reclaim strategy they are used together. Because mark-Compact requires moving objects, it can’t execute very fast, so V8 mainly uses Mark-Sweep for trade-offs, only when there isn’t enough room to distribute objects promoted from the new generation.

incremental Marking

To avoid inconsistencies between the JavaScript application logic and what the garbage collector sees, each of the three basic algorithms for garbage collection needs to pause the application logic until the garbage collection is complete, a behavior known as “stop-the-world.” In V8’s generational garbage collection, a small garbage collection only collects the new generation, and since the default configuration of the new generation is small and there are usually fewer live objects in it, even a total pause doesn’t have much impact. However, older versions of V8 tend to be larger and live, and the pauses caused by the marking, cleaning, collating, and other actions of full garbage collection can be scary and need to be fixed. To reduce the pause time associated with full-heap garbage collection, V8 started with incremental marking, where actions that were supposed to be completed in one stop are broken down into many small “steps” that allow JavaScript application logic to execute for a short time. Garbage collection alternates with application logic until the tagging phase is complete. V8’s incremental markup has reduced the maximum pause time for garbage collection to about 1/6. The V8 sequel also introduces lazy sweeping and incrementalcompaction, making cleaning and sorting actions incremental. It is also planned to introduce parallel tagging and parallel cleaning to further reduce each pause time by taking advantage of multi-core capabilities.

Content reference from “simple Node. Js” Piao Ling, interested in children boots can go to read