GC understand the mechanism of Node.js

"Layman's language Node.js" Chapter 5, "Memory Control" reading notes

With the development of Node, JavaScript application scenario is no longer confined to the browser. This article does not discuss web applications, command-line tools to perform a short time, and only affects the end-user scenarios. Due to the short running time, with the exit, memory release process, it is hardly necessary memory management. But with the Node in the service side of a wide range of applications, JavaScript memory management needs our attention.

V8's memory limit

In general backend development language, there is no limitation on the base used in the memory, but in the Node will find only memory used lower (about 1.4GB 64-bit system memory through the use of JavaScript, the 32-bit system about 0.7GB). Under such restrictions, will result in Node can not directly operate large memory object.

The main cause of the problem is that Node JavaScript execution engine V8.

In V8, all JavaScript objects are to allocate from the heap. Node provides V8 used in the amount of memory to view the method process.memoryUsage().

  • heapTotal It has applied to the heap memory
  • heapUsed The current use of heap memory

Why V8 to limit the size of the heap:

  1. V8 browser designed unlikely to have a scene with lots of memory
  2. Limit garbage collection mechanism of the V8. (According to the official statement, the garbage collection heap memory to 1.5GB example, V8 do a little more than garbage collection takes 50ms, do a non-incremental garbage collection requires more than 1s)

V8 provides an option so that we can control the use of memory size

  • node --max-old-space-size=1700 test.js Set the maximum value of the old generation of memory space in MB
  • node --max-new-space-size=1024 test.js The maximum value of the new generation memory is provided, in KB

Sorry, this two maxima need to perform at startup. This means that the memory used by V8 is no way to automatically expand the use according to the situation, and when the memory allocation process when the limit is exceeded, it will cause an error process.

V8's garbage collection mechanism

V8 garbage collection policy is mainly based on generational garbage collection mechanism. In V8, the main memory into the new generation and the old generation two generations. The new generation of objects in a relatively short time for the survival of the object, the object is in the Older Generation live objects longer or permanent memory.

V8 is a schematic generational

V8 whole heap size is the new generation of memory plus memory space Older Generation

Scavenge algorithm

On the basis of generational, the new generation objects primarily by Scavengegarbage collection algorithm. In the specific implementation Scavenge, the main use of the Cheneyalgorithm.

Cheney algorithm is a copy of the garbage collection algorithm manner of adoption. It will heap into two, each part of the space to be semispace. In both semispace space, only one is in use, the other is in idle. Semispace in use in the space becomes Froma space, the space becomes idle Tospace. When we assign an object, first in the From allocate space. When starting garbage collection, checks the live objects From space, the survival of these objects will be copied to the To space, rather than survival space occupied by the object will be released. After copying, From space, and roles To swap space occurs.

Scavenge drawback is that only use half of heap memory, but Scavenge because the only copy of the object survived, and for the short life cycle of the subject occupies only a small part of the scene alive, so it outstanding performance in terms of time efficiency. Scavenge is a typical sacrifice space for time algorithm can not be applied to all large-scale garbage collection, but very suitable for use in the new generation.

V8 diagram of heap memory

Promotion

Moving an object from the new generation into the old generation in a process called promotion.

From the live objects in the space prior to the To copy space needs to be checked, under certain conditions, it is necessary to move a long survival period of the old generation to objects in the object is complete promotion.

There are two main conditions for promotion:

  1. Whether the object experienced a recovery Scavenge
  2. To space has been used for more than 25%

Setting this limit worth 25% of the reason is that when the Scavenge recovery is complete, this will become a space To From space, the next memory allocation will take place in this space, if the proportion is too high, it will affect subsequent memory allocation.

Mark-Sweep & Mark-Compact

V8 in the old generation mainly uses a Mark-Sweep mode, and Mark-Compact combination of garbage collection.

Mark-Sweep is labeled clear meaning, it is divided into two stages, mark and sweep. Mark-Sweep all objects in the heap traversal mark phase, and mark objects alive in the subsequent purge stage, the object is not only clear the mark.

The biggest problem is the Mark-Sweep scavenged, the memory space is not continuous state will be making a mark. This memory fragmentation have caused subsequent memory allocation problem, because the situation needs to allocate a large object is likely to occur, then all of space debris are unable to complete the assignment, it will advance to trigger garbage collection, recycling and this is unnecessary.

To solve the problem of memory fragmentation of Mark-Sweep, Mark-Compact was proposed. Mark-Compact is the meaning of tags to organize, in the Mark-Sweep foundation staged and then come. Their difference is that the object is marked for death when, in the finishing process, the live objects to the end of the movement, after the move is complete, clean out the memory directly outside the boundary.

The following table is a simple comparison of the three main garbage collection algorithm

As can be seen from the table, and between the Mark-Sweep Mark-Compact, since the Mark-Compact need to move the object, so that execution speed can not be quickly, so the choice, V8 Mark-Sweep is mainly used, in the space not enough promotion from the new generation coming of objects when assigned using Mark-Compact.

Incremental Marking

In order to avoid the application logic and JavaScript garbage collector inconsistent see the emergence of three kinds of garbage collection algorithms need to pause the application logic, this behavior is called "full stop" (stop-the-world).

Due to the small space configuration of the new generation, less live objects, full stop has little effect on the new generation. But the larger space of the old generation is typically configured, and more live objects, labeled full of garbage recycling (full garbage collection), clear, pause finishing action that will be more terrible.

In order to reduce the dwell time full of garbage recycling brings, V8 from the tag first phase to start, pause to breath supposed to complete the action into incremental markings (Incremental Marking), which is split into many small "step" each finished a "step" let JavaScript application logic performed until a little while the mark phase is completed, garbage collection and application logic alternately performed.

V8 after a marked improvement incremental garbage collection pause maximum time can be reduced to about 1/6 of the original.

GC View Log

View log garbage collection is mainly a way to add startup --trace_gcparameters.

summary

  1. Node JavaScript execution engine as the V8, memory usage and control is also limited by the V8.
  2. V8 into the memory of the old generation and the new generation, objects were stored longer survival time and shorter survival time or permanent memory.
  3. Scavenge for use in the new generation garbage collection algorithm, the advantage is speed no memory fragmentation, the disadvantage is occupied twice as much memory space.
  4. In the old generation Mark-Sweep and Mark-Compact two algorithms used in combination, using the Mark-Sweep mainly, the advantages of no moving objects, the disadvantage is memory fragmentation. Mark-Compact is a complement to the Mark-Sweep, sorting memory space is insufficient when new objects are allocated for promotion, clear memory fragmentation, due to moving objects, slower.
  5. Use Incremental Marking V8 to reduce the impact of a full stop brings.

Guess you like

Origin www.cnblogs.com/chaohangz/p/10963565.html