Understanding Java Heap: Memory Regions, GC Generations, and Allocation Strategies
This article explains the Java heap structure, generational layouts in JDK 1.7 and 1.8, the G1 collector's region-based approach, and detailed object allocation rules including Eden placement, large object handling, promotion thresholds, and space allocation guarantees.
For Java applications, the Java heap is the largest memory area managed by the JVM, shared by all threads and created at JVM startup to store object instances.
JDK 1.7 Generational Structure
In JDK 1.7 and earlier, the heap is divided into three parts: Young Generation, Old Generation, and Permanent Generation. The Young Generation further consists of Eden and two Survivor spaces.
JDK 1.8 Generational Structure
Starting with JDK 1.8, the Permanent Generation is removed and replaced by Metaspace, which automatically expands. The heap now consists of Young and Old generations without a fixed Permanent Generation.
The runtime data area layout is shown below:
G1 Collector
G1 removes the physical separation of Young and Old generations, dividing the heap into many equal-sized regions. Some regions belong to the Young generation, and G1 still performs copying collection for those regions. The collector also supports Humongous regions for objects larger than 50% of a region size.
Object Memory Allocation
The allocation process is illustrated below.
Objects Preferentially Allocated in Eden
Most objects are allocated in the Eden space of the Young generation. When Eden is full, a Minor GC is triggered. The HotSpot JVM can print detailed GC logs with the -XX:+PrintGCDetails flag.
/**
* -XX:+PrintGCDetails
*/
public class GCTest {
public static void main(String[] args) {
byte[] allocation2 = new byte[8000 * 1024];
}
}Sample output of the heap after running the program:
Heap
PSYoungGen total 38400K, used 11353K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000)
eden space 33280K, 34% used [0x0000000795580000,0x00000007960966f8,0x0000000797600000)
from space 5120K, 0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000)
to space 5120K, 0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000)
ParOldGen total 87552K, used 0K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000)
object space 87552K, 0% used [0x0000000740000000,0x0000000740000000,0x0000000745580000)
Metaspace used 3017K, capacity 4556K, committed 4864K, reserved 1056768K
class space used 319K, capacity 392K, committed 512K, reserved 1048576KThe output shows that allocation2 was allocated in the Eden space.
Large Objects Directly Enter Old Generation
Objects larger than a threshold set by -XX:PretenureSizeThreshold are allocated directly in the Old generation (effective with Serial and ParNew collectors).
Long‑Lived Objects Move to Old Generation
Objects that survive multiple Minor GCs increase their age counter; once the age reaches the MaxTenuringThreshold (default 15) they are promoted to the Old generation.
Dynamic Object Age Determination
If the total size of objects of a certain age in Survivor exceeds half of the Survivor space, those objects may be promoted early, without waiting for the age threshold.
Space Allocation Guarantee
Before a Minor GC, the JVM checks whether the Old generation has a contiguous free region large enough to accommodate all surviving objects from the Young generation.
Reference Information
《深入理解 JVM 虚拟机-第三版》周志明
https://www.infoq.cn/article/Java-PERMGEN-Removed
https://www.cnblogs.com/wangzhongqiu/p/10250868.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
