Virtual machine memory allocation and recycling strategy (12)

Under the classic generational design, new-born objects are usually allocated in the young generation. In a few cases (the object size exceeds the threshold), they may also be directly allocated in the old generation.

Objects are allocated in Eden first

In most cases, objects will be allocated in the Eden area of ​​the new generation. When there is not enough space in the Eden area, the virtual machine initiates a Minor GC.
-XX:+PrintGCDetails tells the virtual machine to print the memory recovery log when garbage collection occurs, and output the current memory area allocation when the process exits.
-XX:SurvivorRatio=8 determines the space ratio of the new generation Eden area to a Survivor area is 8:1.
Example:
When the Eden area is 8M in the new generation, both Survivor areas are 1M.
Assuming that the young generation is 10M (the available space is 9M=8M Eden area and a 1M Survivor area), the old generation is 10M, when three 2M objects are allocated to the Eden area, if you allocate a 4M object Because the space of the young generation is insufficient, all minor GCs occur, but the virtual machine finds that the remaining 1MSurvivor area is not enough to store 6M objects, so the initial three objects have to be transferred to the old area in advance through the allocation guarantee mechanism.

Large objects directly enter the elderly area

Virtual machine parameters: -XX:PretenureSizeThreshold , specify that objects larger than this setting are directly allocated in the elderly area.
Purpose : Avoid copying back and forth between the Eden area and the two Survivor areas, resulting in a large number of memory copy operations.

Long-lived objects enter the old age

The virtual machine defines an object age counter for each object in the object header. If the object is born in the Eden area, it survives after a Minor GC and can be stored in the Survivor area, the object will be moved to the Survivor area and Age is added One. When its age increases to a certain level, it will be promoted to the elderly area. The default is 15.
Virtual machine parameters: -XX:MaxTenuringThreshold is set to the age threshold of the object in the newborn zone.

Dynamic object age determination

The HotSpot virtual machine does not always require objects in the newborn zone to enter the old zone after reaching the threshold. If the total size of all objects of the same age in the Survivor space is greater than half of the Survivor zone space, all objects whose age is greater than or equal to the object can be directly Enter the old age.

Space allocation guarantee

Before the occurrence of Minor GC, the virtual machine must check whether the maximum available continuous space in the old age is greater than the total space of all objects in the new area. If the condition is established, then this time Minor GC can be guaranteed to be safe. If not, the virtual machine will first Check whether the setting value of the -XX:HandlePromotionFailure parameter allows guarantee failure; if allowed, check whether the maximum continuous available space of the old generation is greater than the average size of the objects promoted to the old generation , if it is greater than the average size of the object , perform a Minor GC (also risky, this time The promotion space is greater than the previous average), if it is less than, or the guarantee risk is not allowed, this is to be changed to a FULL GC.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109320326