Several memory allocation strategies of JVM

1. Objects are allocated in Eden first.

In most cases, objects are allocated in the young generation Eden area. When there is not enough space in the Eden area, the virtual machine will initiate a Minor GC.

PS:

Cenozoic GC (Minor GC): The GC that occurs in the Cenozoic, most of which are exclusively owned by Java, will die soon, so the Minor GC is very frequent and the recovery speed is also fast;

Old generation GC (Major GC / Full GC): The GC that occurs in the old generation is more than 10 times slower than the Minor GC.

2. Large objects enter the old age directly.

Large objects are Java objects that require a lot of contiguous space, such as very long strings and arrays. Large objects often appear, and there is still a lot of space in memory to trigger GC in advance to obtain enough continuous space to place them.

3. Long-lived objects will enter the old age.

When a virtual machine is recycled, it must be able to identify which objects are placed in the young generation and which objects are placed in the old generation, so that the idea of ​​generational collection can be adopted. The virtual machine defines an object age counter for each object. If the object is still alive after Eden is born and after the first Minor GC, and can be accommodated by the Survivor, it will be moved to the Survivor space, and the object age is set to 1. Every time an object goes through Minor GC in Survivor, its age increases by 1. When the age increases to a certain level (the default is 15 years old, this parameter can be adjusted), it will be promoted to the old age.

4. Dynamic object age determination.

The virtual machine does not always require that the age of an object must reach a certain value to be promoted to the old generation. If the sum of the sizes of all objects of the same age in the Survivor is greater than half of the Survivor space, objects with an age greater than or equal to this age can directly enter the old generation.

5. Space Allocation Guarantee.

Before sending the Minor GC, the virtual machine first checks whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation. If this condition is true, it can ensure that 100% of the objects in the Minor GC are alive. In this extreme case, All these surviving objects can be transferred to the old age, then Minor GC is safe at this time. If the condition does not hold, the virtual machine checks whether the guarantee is allowed to fail. If it is allowed, it continues to check whether the maximum available continuous space in the old generation is greater than the average size of the objects promoted to the old generation each time. If it is greater, it will try a Minor GC; if is less than (or the setting does not allow guarantee failure), then a Full GC is performed instead.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326393091&siteId=291194637