Detailed explanation NodeJS memory governance

The host environment is running, the corresponding requirements for memory governance, when the host environment is a browser, due to the short running time of the web, and only runs on the user’s machine (equivalent to distributed) Even if the memory is used too much or there is a certain memory leak, it does not have a much more impact on the end user. When the host environment programming server (Node), the situation is large, the code is running on a fixed machine (centrally), and the time run is running for a long time, once memory governance is not good, memory expansion Even if the memory leaks, the server-side response time will occur and even the service Crash.

NodeJS is based on V8 build, so the JavaScript object used in Node is basically (Buffer is not) allocation and management through V8. V8 is restricted in the size of the memory (64-bit operating system, the maximum heap memory size can be used for a single Node process is approximately 1.5GB). Even if the server’s memory is large, because this limit of V8, Node cannot fully utilize the resources of the server. Even so, why is V8 to do so? The reason for doing such a restriction is actually related to the garbage collection mechanism. Taking 1.5GB of garbage collection stacks as an example, V8 is a small garbage collection requires more than 50ms, do a full amount of garbage recycling or even more than 1s, to know garbage During the recycling process, the JavaScript thread is to be suspended, too long, for the performance of the backend service, has a large impact on the performance of the backend service, so it is limited to the heap memory for this regard. Even so, V8 also provides a way to customize the size of the stack memory, and Old-Space represents the old born, New-space represents the new generation.

Node –Max-Old-Space-size = xxx index.js // can also pass -max-new-space-size before MB // Define new generation of bongeons, now it is no longer
  
When the server is frequently restarted due to memory leakage, it is recommended to first try the time for positioning problems. After all, the service response is slow to return to the user, which is better to accept the user.
Why do you need a born and new generation?

The old and new generation is actually different from the divided garbage recovery mechanism, because there is no garbage collection algorithm to compete for all scenes, different objective cycles need different The recycling strategy can achieve the best results, so V8 uses the independent garbage collection mechanism, and the target is different from the survival time of the object, and then the memory of different departments (new generation, the elderly) is more suitable. Also a better algorithm.

The survival time in the new generation is short, and the object survival time in the old generation is even longer, and even the resident memory. Based on this, the new generation of new generation is generally more than the old birthday memory, and the new generation of new generation in V8 is 32M (64-bit system as an example), and the maximum amount of memory is 1400MB. V8 actually used the memory size is the sum of the new generation + the amount of memory used by the old generation (1432MB), but the maximum value of the V8 is actually 32m (1464MB)


newborn Why do you do garbage recovery?

The new generation of garbage collection algorithms called Scavenge. In the specific implementation of Scavenge, the Cheney algorithm is mainly used, and the Cheney Algorithm is divided into two, one use (from semispace), an id semispace. When you create an object, now the FROM space is allocated. When you need garbage collection, check the survival object in the FROM space, then copy the living object to the To TO space, and empty the FROM space, and interchange from from and TO The entire garbage collection process is to copy the survival object between two SEISPACEs. Scenario survival objects in lifecycle are relatively small in the entire object, so Scavenge uses copying survival objects, but Scavenge can only utilize stack memory.Half of space, this is a typical manifestation of space transfer time.

When an object is still survived by multiple garbage, it is considered to be a longer life cycle. On the one hand, the new generation of new generation is relatively small, on the other hand, repeat the life cycle. Objects are also very efficient, so objects with long life cycle will be moved to the old generation. The new generation of objects move to the old birth generation. There are two objects: 1. Whether the object is a longer life cycle (have undergone garbage recovery) 2. The TO space usage is more than 25%. The reason for the limited 25% is that since the TO will become from from the garbage collection completion, if the restriction may occur, the situation in which the FROM is quickly used, and frequent garbage recovery will affect efficiency.

How to do garbage recovery?

The old generation is not suitable for operation of survival objects due to survival objects, and it is not suitable for the survival object. It is not appropriate to use the Scavenge algorithm, so the old generation uses Mark-SWEEP and Mark-Compact. The way.

Mark-SWEEP is divided into two phases, traversed all objects in the tag phase, and tag the living object, and then the object that is not marked during the clearance phase will be cleared. Mark-SWEEP solves the problem of memory release but because there is no operation like Scavenge, the memory is not continuous. Mark-Compact is used to solve the problem of internal presence. Mark-Compact moves one end to one end, and directly cleans up the memory outside the boundary, so that there is a continuous use memory of the large segment, but due to the movement of the object, the speed of Mark-Compact is comparable. Mark-sweep is slow. V8 mainly uses Mark-SWEEP, which only uses Mark-Compact when space is not sufficient to assign objects in the new generation. The garbage collection process will cause the application to suspend execution, because the new generation itself is small, and the survival object to be replicated is small, even if the full amount of garbage is not affected, but the old generation SpaceThere are also many survival objects. Perform a full amount of garbage collection is a relatively long time for the application pause, so V8 changes the old label to the incremental update, so that the tag and the application alternately execute until the tag is completed, then Garbage recycling re-executes the cleanup work. Note that cleanup work is not incremental.

Can developers specify to force garbage collection?

The answer is OK, use -expose-gc flag

$ node – GC File when starting Node service

Such a global object has a function of performing garbage recovery

 Global.gc ();   
Recommended safer write

Function forceg () {(global.gc) {global.gc ( } else {console.warn (‘NO GC Hook! Start your program as `node –Expose-gc file.js`’);}}
   
Finally, share the reference:
https://speakerdeck.com/addyosmani/javascript-memory-management-masterclass

https://www.jb51.net/Article/140005 .htm
https://www.xarg.org/2016/06/FORCING-GARBAGE-COLLECTION-IN-NODE-JS-and-javascript /
                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment