<> When does the object enter the elderly generation ?
* Large objects enter the elderly generation directly .
The virtual machine provides a threshold parameter , Make objects larger than this setting be allocated directly in the older generation . If large objects enter the Cenozoic era , The new generation uses replication algorithms to collect memory , Will result in Eden Area and two Survivor A large number of memory copies occur between partitions , This should be avoided .
* Long lived objects enter the elderly generation . The virtual machine defines an age counter for each object , Object in Eden District birth , Once Minor
GC Still alive after , And can be Survivor If the area can accommodate , Will be moved to Survivor Middle area , The age of the object is set to 1. Then the object is Survivor Every time in the district Minor
GC, Age increases 1, When the age exceeds the set threshold , Will be moved to the older generation .
* Dynamic object age determination ： If in Survivor All objects of the same age in the space , Size sum greater than Survivor
Half the space , Then objects older than or equal to this age will directly enter the elderly generation , You don't have to wait until the age required in the threshold .
* Space allocation guarantee ： If the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation , that Minor GC
It's safe . If the maximum available continuous space in the old generation is greater than the average size of objects promoted to the old age in previous generations , Just do a risky test Minor GC, If less than average , Proceed Full GC
To make more space for the elderly . Because the new generation uses replication algorithm , For memory utilization , Use only one of them Survivor Space for rotation backup , So if a large number of objects are Minor GC
Still alive after , cause Survivor
Not enough space , Through the distribution guarantee mechanism , Transfer the extra objects to the old age in advance , However, the premise to guarantee in the old age is that they still have the remaining space to accommodate these objects , Because we can't know in advance how many objects will survive , Therefore, take the average size of the objects promoted to the old age as the empirical value , Compare with the remaining space of the elderly generation .