NOTE
7.9 Object Allocation Rules
Historical rules and flow for object allocation in the JVM.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Rules
- Objects are preferentially allocated in the Eden area. If there is not enough space in Eden, the virtual machine performs a Minor GC.
- Large objects directly enter the old generation (large objects are objects that require a large amount of contiguous memory). The purpose is to avoid large amounts of memory copying between Eden and the two Survivor areas (the young generation uses a copying algorithm to collect memory).
- Long-lived objects enter the old generation. The virtual machine defines an age counter for each object. If an object survives one Minor GC, it enters a Survivor area. After each subsequent Minor GC, the object’s age increases by 1 until it reaches the threshold and enters the old generation.
- Dynamically determine object age. If the total size of all objects of the same age in the Survivor area is greater than half of the Survivor space, objects whose age is greater than or equal to that age can directly enter the old generation.
- Space-allocation guarantee. Each time a Minor GC is performed, the JVM calculates the average size of objects moved from Survivor to the old generation. If this value is greater than the remaining space in the old generation, a Full GC is performed. If it is smaller, check the
HandlePromotionFailuresetting. If true, only Minor GC is performed; if false, Full GC is performed.
2. Allocation Flow

- If no escape occurs, allocate on the stack.
- If it is a large object, allocate it in the old generation.
- If TLAB is enabled and the object is smaller than the TLAB, allocate it in the TLAB (the TLAB is in the young generation, but it is a small space owned by the thread itself, so creating an object there does not require locking the entire young generation); otherwise, allocate directly in the thread-shared space of the young generation.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub