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.
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)
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?
$ node – GC File when starting Node serviceSuch a global object has a function of performing garbage recovery
Global.gc ();Recommended safer write
Finally, share the reference: