Keywords: Java Heap Memory | Garbage Collection | Young Generation | Old Generation | Permanent Generation | JVM Memory Management
Abstract: This article provides an in-depth analysis of Java Virtual Machine heap memory concepts, detailing the partitioning mechanisms of young generation, old generation, and permanent generation. Through examination of Eden space, survivor spaces, and tenured generation garbage collection processes, it reveals the working principles of Java generational garbage collection. The article also discusses the role of permanent generation in storing class metadata and string constant pools, along with significant changes in Java 7.
Basic Architecture of Java Heap Memory
The Java Virtual Machine (JVM) heap memory employs a generational management strategy, designed based on the important observation that most objects have short lifetimes. According to object survival characteristics, heap memory is divided into different regions, each employing distinct garbage collection strategies.
Young Generation
The young generation is a specialized area within heap memory for storing newly created objects. This region is further subdivided into three components:
- Eden Space: This is where objects are initially allocated. When programs create objects using the
newkeyword, memory is first allocated from Eden space. - Survivor Space: Consists of two equally sized regions (From and To spaces), used to store objects that survive garbage collection from Eden space.
The young generation employs a copying algorithm for garbage collection. When Eden space becomes full, it triggers Minor GC. Surviving objects are copied to the From space of survivor space, and the From and To spaces swap roles during each GC cycle.
Old Generation
The old generation, also known as tenured generation, is used to store long-lived objects. Objects are promoted from young generation to old generation under the following conditions:
- Objects survive a specific number of garbage collection cycles in the young generation
- Objects are too large to fit into survivor space
- Survivor space is full
The old generation uses mark-sweep or mark-compact algorithms for garbage collection, a process known as Major GC or Full GC.
Permanent Generation
It is particularly important to note that in Oracle's JVM implementation, the permanent generation is not part of the heap memory but a separate storage area. The permanent generation primarily stores:
- Class metadata (class definitions, method objects, etc.)
- In Java 6 and earlier versions, also included interned strings from the string constant pool
- Reflective data of the virtual machine itself
In Java 7, a significant change moved interned strings from the permanent generation to the main object heap. The size of permanent generation can be configured using the JVM parameter -XX:MaxPermSize.
Interactions Between the Three Generations
Java's generational garbage collection mechanism achieves efficient memory management through object promotion between different generations:
- Newly created objects are first allocated in the young generation's Eden space
- Objects surviving Minor GC are moved to survivor space
- Objects surviving multiple GC cycles in survivor space are eventually promoted to old generation
- Class metadata stored in permanent generation typically remains unchanged throughout the application lifecycle
This generational design allows the JVM to employ different collection strategies for objects with varying lifetimes: the young generation uses efficient copying algorithms, while the old generation uses algorithms more suitable for long-lived objects.
Practical Applications and Tuning Recommendations
Understanding these concepts is crucial for JVM performance tuning:
- Setting young generation size too small leads to frequent Minor GC
- Insufficient old generation size causes frequent Full GC, severely impacting performance
- Permanent generation overflow is often related to excessive dynamic class loading
- Monitoring usage across generations helps identify memory leak issues