Java
Heap Memory
Programming
Garbage Collection
Memory Management

Java heap terminology young, old and permanent generations?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In Java, memory management is a crucial aspect of application performance and stability. The Java Virtual Machine (JVM) manages memory through different memory pools or generations: the Young Generation, the Old Generation, and the Permanent Generation. Understanding these segments is essential for optimizing performance and avoiding common pitfalls like memory leaks or frequent garbage collections.

Young Generation

The Young Generation is where all new objects are allocated when they are created. Because most objects in Java are short-lived, this area is frequently reclaimed. The Young Generation is further divided into three parts: one Eden space and two Survivor spaces (S0 and S1).

Eden Space: This is where new objects are initially allocated. When a garbage collection (GC) event occurs, still-alive objects are moved from Eden to one of the Survivor spaces. Survivor Spaces: These two spaces hold objects that have survived at least one GC cycle. Objects are moved from one Survivor space to the other (i.e., from S0 to S1 and vice versa) each time a minor GC occurs, meaning not every GC involves the Old Generation. This mechanism helps in filtering out those objects that are more likely to die young, reducing the overhead on older parts of memory.

Garbage collection in the Young Generation is typically fast because it deals only with short-lived objects and uses a copying algorithm where live objects are copied to a survivor space, and the rest are discarded.

Old Generation

The Old Generation (or Tenured Generation) is used to store objects that have existed for some time in the survivor space. As space in the survivor areas fills up, objects that survived enough garbage collection cycles in the Young Generation are moved to the Old Generation. This transfer happens during a major GC event, which is typically more time-consuming than the minor GC events of the Young Generation because it involves more data and uses a different algorithm, typically a mark-sweep-compact or mark-and-sweep approach, which identifies live objects and clears out unused data without copying live data elsewhere.

Permanent Generation

The Permanent Generation (or PermGen) has been a feature of the JVM used to store the JVM’s metadata which includes class and method objects. However, from Java 8 upwards, this memory space has been replaced by Metaspace. Unlike PermGen, which has a fixed size, Metaspace expands dynamically, reducing the need to tune the size and avoiding out-of-memory errors due to metadata storage.

Generational Garbage Collection

This separation into different generations allows the JVM to optimize garbage collection. The rationale is that organizing memory into these segments matches the typical behavior of applications where most objects die young (e.g., temporary variables used within the scope of a method), a smaller number live a bit longer, and very few objects need to live for the duration of the application runtime.

Practical Impact and Monitoring

For developers and system administrators, understanding the role and behavior of each generation can help in tuning the JVM for better performance. Monitoring tools such as VisualVM, jConsole, and various command-line tools can provide visibility into how much data is in each generation, the frequency, and duration of garbage collections. This information can be critical in detecting memory leaks and in fine-tuning the garbage collector's behavior to balance throughput and latency.

For example, a web application with many short-lived sessions may benefit from a larger Young Generation space to minimize the frequency of major GC cycles impacting response times.

Summary Table

GenerationDescriptionGC TypeKey Points
YoungWhere new objects are placed. Comprises Eden and two survivorsMinor GC (Frequent)- Fast GC - Objects rarely survive long here
OldFor objects that have existed longerMajor GC (Less frequent)- Slower but less frequent GC - Object promotion occurs
Permanent (Obsolete in Java 8+)Held JVM metadata pre-Java 8Major GC- Replaced by Metaspace in Java 8+

By strategically managing these memory areas and understanding their characteristics, developers can significantly improve the efficiency and performance of Java applications.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.