Why does Java GC need to check the largest contiguous space available in old times when allocating guarantees?

see a paragraph:

< H2 > before Minor GC occurs, the virtual machine checks whether the largest contiguous space available in the old era is larger than the total space of all objects in the new generation. If this condition is true, then Minor GC can ensure that it is safe. If not, the virtual machine checks to see if the HandlePromotionFailure setting allows the guarantee to fail. If allowed, it will continue to check whether the maximum available contiguous space in the old age is greater than the average size of the object promoted to the old age < / H2 >

Why is the continuous space larger than the total space of all objects in the new generation? How do I think it is that the continuous space of the old era is greater than the space needed by the largest object of the new generation, rather than the "total space of all objects of the new generation"? Do all new generation objects have to be placed in a continuous space?

and the sentence "continue to check whether the largest contiguous space available in the old age is larger than the average size of objects promoted to the old age". Does this average size refer to the average size of a single object or the average size of the sum of all objects?

Mar.04,2021

this sentence comes from the second edition of in-depth understanding of the Java Virtual Machine. You need to finish reading the garbage collection algorithm in this book to know why. To put it simply, what if all the objects of the new generation are not recyclable on the chain?

there are usually two algorithms for determining which objects can be recycled, one is reference counting, the other is reachability analysis algorithm. Hotspot uses the second. The second one passes through some objects called GC Root. The objects that can be used as GC Roots include the following:

  • the object referenced in the virtual machine stack (the local variable table in the stack frame).
  • objects referenced by class static properties in the method area
  • The object referenced by a constant in the
  • method area.
  • objects referenced by JNI (commonly known as Native methods) in the local method stack

these references form a chain, and all instructions on the chain are still in use and cannot be recycled.


question one, since it is a guarantee, it is necessary to ensure 100% success. If there are many free areas in the old age, but they are all so small that they are not small enough to allocate an object, then there is a lot of room in the old age, and it cannot be guaranteed, and it must be continuous. In addition, the object of the new generation may be collectively ascended to the old age at some time, and in order to guarantee that the idle size of the old generation must be larger than that of the whole younger generation.
Synthesis, the maximum continuous free space of the old generation needs to be larger than the total space of the younger generation.

question 2, the sum of the size of all objects promoted to the old age. The average here only averages the sum of the size of the younger generation of objects that are promoted each time ygc.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9752c-4741d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9752c-4741d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?