Node.js memory allocation and garbage collection

Brief memory allocation and garbage collection of Node.js

Memory Allocation

Node.js is a C ++ program JavaScript V8 engine controlled by a
program in a memory management operation V8 is usually represented by allocating a portion of memory space. This space is called resident set (Resident Set).
V8's memory management scheme is somewhat similar to the Java Virtual Machine (JVM), it will be segmented memory:

  • Code Code: actual code to be executed
  • Stack Stack: includes all value types (primitive types, such as integers and Boolean) carries the pointer references the heap object, and a pointer defined program control flow.
  • Heap Heap: for holding a reference type (including an object, a string and closures) memory segment

View memory usage:

       
       
1
       
       
console.log(process.memoryUsage());

Garbage collection

V8's garbage collection mechanism generational garbage collection, based on this mechanism, the memory is divided into the new generation of V8 (New Space) and the old generation (Old Space).
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.
-Max-old-space-size setting command is the maximum value of the old generation of memory, and -max-new-space-size command may set the size of the new generation of memory space.

Garbage collection algorithm

In V8, according to survival time of the object memory garbage collection different generational, and then were subjected to a more efficient algorithm for different memory.
The new generation of garbage collection in the new generation, mainly through Scavenge garbage collection algorithm.

Scavenge

Scavenge algorithm heap memory into two, each part of the space is called the semispace. In these two semispace space, only one is in use, the other is idle.
Semispace in use is referred to as a semispace From space, idle space is called To. When we assign objects from the first allocation From space. When starting garbage collection,
checks the object From space survival, the survival of these objects are copied to the To space, rather than the survival of the space occupied by the object will be released. After copying, From Space To Space and the roles were reversed.
In the process of garbage collection, the objects will survive is through replication between two semispace space.

How the new generation to the old generation of objects

Survive long periods in the new generation object will be moved to the old generation, required to meet one of two conditions:

  1. Whether the object experienced Scavenge recovery.
    From an object copy from the space into the space To, address checks its memory to determine whether the object has undergone a recovery Scavenge, if you have experienced, then the objects from the old generation to the space From space.
  2. To share the memory space of more than 25% limit.
    When an object is copied from the From To space to the space, if the space has been To use more than 25%, then the object is directly copied to the old generation. The reason for this is that 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.

So, V8 in the old generation mainly by way of "Mark-Sweep" algorithm "Mark-Compact" algorithm combination of garbage collection.
Older Generation garbage collection in the Older Generation for an object, due to the larger proportion accounted for live objects,
use Scavenge algorithm is clearly unscientific. Too many to a copy of the object will cause efficiency problems, and secondly, the need to waste space twice as many.

Mark-Sweep

Mark-Sweep is a clear mark of meaning, mark and sweep into two stages. All objects in the heap traversal mark phase, and mark live objects, in the subsequent purge stage, only remove objects outside the mark.
But Mark-Sweep has a very serious problem, is to conduct a marked recovery after clearing, memory can become fragmented. If you need to allocate a large object, this time will not be able to complete the assignment. At this time in relation to Mark-Compact he played.

Mark-Compact

Mark-Compact is finishing mark meaning that evolved in the Mark-Sweep basis. Mark-Compact after marking live objects 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.

Incremental markings (Incremental Marking)

Given the characteristics of Node single-threaded, V8 garbage collection every time, we need to apply logic to pause, to be completed before the implementation of garbage collection recovery application logic, is called full stop.
In generational garbage collection, the garbage collection is only a small new generation, and the survival of the object is relatively small, even if the full stop is not much impact. But in the old generation, the more live objects,
garbage collection marking, cleaning, sorting requires a long pause, this will seriously affect the performance of the system. So increment flag is raised. It starts with the mark phase,
the breath is supposed to pause the action to complete an incremental change mark, split into many small "stepping", each done a "stepper" let the JavaScript application logic to perform a little while,
garbage such recovery and application logic performed alternately until the completion of the mark phase.

Memory leak troubleshooting tool

  • node-heapdump
    it allows for a snapshot V8 heap memory for post hoc analysis.
  • node-profiler
    it is crawling heap snapshot tool alinode team produced a similar node-heapdump. Tutorial: How to Use Node Profiler

Original: Large columns  of memory allocation and garbage collection of Node.js


Guess you like

Origin www.cnblogs.com/chinatrump/p/11614933.html