Objects are allocated in Eden first
In most cases, objects are allocated in the Eden region of the new generation. When the Eden area does not have enough space to allocate, the virtual machine will initiate a Minor GC.
Analyze the above case:
The testAllocation () method of this code attempts to allocate three 2MB objects and one 4MB objects, and limits the Java heap size to 20MB by using -xMS20m, -XMx20m, and -xmn10m at runtime. Of this, 10MB is allocated to the new generation and the remaining 10MB to the old generation. XX: – SurvivorRatio=8 determines that the ratio of Eden area to Survivor area is 8:1 in the Cenozoic era. Information of “Eden space 8192K, from space 1024K, to space 1024K” can also be clearly seen from the output result. The total available space of the new generation is 9216KB (the total capacity of Eden area + 1 Survivor area).
A Minor GC occurs when testAllocation () allocates the allocation4 object. The result of this GC is that the 6651KB of the new generation becomes 148KB. The total memory footprint was barely reduced (because allocation1, Allocation2, and Allocation3 were all alive, the virtual machine found few recyclable objects). The reason for this Minor GC is that when allocation4 was allocated memory, it was discovered that Eden had already been occupied by 6MB, and the remaining space was not enough to allocate the 4MB memory required by Allocation4, so the Minor GC occurred. During GC, the virtual machine found that three existing 2MB objects could not fit into Survivor space (Survivor space is only 1MB in size), so it had to be migrated to the old age through allocation guarantee mechanism.
Is there any difference between the multiple Minor and Full GC?
Minor GC: Refers to garbage collection that occurs in the new generation. Since Java objects tend to be ephemeral, Minor GC is very frequent and generally fast.
Old GC (Major GC/Full GC) The Applicability of the Parallel Avenge Avenge a Major GC, often accompanied by at least one Minor GC (but not always, as in the Parallel Avenge strategy). Major GC is typically 10 times slower than Minor GC.
Big object goes straight to the old age
Large objects are Java objects that require a large amount of contiguous memory. The most typical large objects are long strings and arrays
Long-lived objects will enter the old age
Virtual machines use the idea of generational collection to manage memory, so the memory collection must be able to identify which objects should be placed in the new generation and which objects should be placed in the old age. To do this, the virtual machine defines an object Age counter for each object. If the object is still alive after Eden’s birth and after the first Minor GC and can be accommodated by Survivor, it is moved to the Survivo space and the object age is set to 1. Each time an object “survives” a Minor GC in Survivo r, its age increases by one year, and when it reaches a certain age (15 by default), it will be promoted to the old age. The age threshold for the object to be promoted to the old age can be set by using -xx: MaxTenuringThreshold.
Dynamic object age determination
In order to better adapt to the memory conditions of different programs, the virtual machine does not always require that the object age must reach MaxTenuringThreshold to advance to the old age. If the sum of all object sizes of the same age in the Survivor space is greater than half of the Survivor space, Objects older than or equal to this age can go directly to the old age without waiting until the age specified in MaxTenuringThreshold.
Space allocation guarantee
Before Minor GC occurs, the virtual machine checks to see if the maximum available contiguous space of the old generation is greater than the total space of all objects of the new generation. If this condition is true, then Minor GC is guaranteed to be safe. If this is not true, the virtual machine checks the HandlePromotionFailure setting to see if the guarantee failure is allowed. If so, it continues to check whether the maximum available contiguous space of the old age is greater than the average size of the objects promoted to the old age, and if so, a Minor GC is attempted, although this Minor GC is risky; If less than, or if the HandlePromotionFailure setting does not allow risk, then do a Full GC instead.