The JVM Heap is divided into different memory areas to manage object lifecycles efficiently. The main divisions are:
- Young Generation – Short-lived objects
- Old Generation – Long-lived objects
- Permanent Generation (or Metaspace in Java 8+) – Class metadata storage
1️⃣ Young Generation (Eden + Survivor Spaces)
✅ What is it?
- Stores newly created objects.
- Objects here have short lifespans (e.g., local variables, temporary objects).
- Most objects die quickly, making GC frequent.
🔥 Structure:
- Eden Space → New objects are allocated here.
- Survivor Space (S0, S1) → Objects that survive GC move between these two spaces.
♻ Garbage Collection:
- Uses Minor GC (fast and frequent).
- Objects that survive multiple Minor GCs are moved to the Old Generation.
📌 Example:
void createObjects() {
String name = "Java"; // Stored in Eden
int[] arr = new int[100]; // Stored in Eden
}
name
and arr
exist only during method execution, and after that, GC removes them.
2️⃣ Old Generation (Tenured Generation)
✅ What is it?
- Stores long-lived objects (e.g., cached data, static objects).
- Objects that survive multiple Minor GCs in the Young Generation move here.
♻ Garbage Collection:
- Uses Major GC (Full GC), which is less frequent but more expensive.
- If Old Generation fills up, OutOfMemoryError (
java.lang.OutOfMemoryError: GC overhead limit exceeded
) may occur.
📌 Example:
static Map cache = new HashMap<>();
cache.put(1, "PersistentData"); // Stored in Old Generation
The cache
object lives throughout the program execution.
3️⃣ Permanent Generation (PermGen) or Metaspace (Java 8+)
✅ What is it?
- Stores class metadata, method data, and static variables.
- Before Java 8: It was called PermGen (fixed size, could cause
OutOfMemoryError
). - Java 8 and later: Replaced by Metaspace, which dynamically grows.
📌 Example:
class Car {
static String type = "Sedan"; // Stored in Metaspace
}
type
is a static variable, so it’s stored in Metaspace.
Differences Between PermGen & Metaspace
Feature | PermGen (Before Java 8) | Metaspace (Java 8+) |
---|---|---|
Storage | Fixed-size (can run out of space) | Dynamically growing |
Garbage Collection | GC cleans it but can cause OOM errors | Dynamically managed |
Configurable? | Yes (-XX:MaxPermSize ) | Yes (-XX:MaxMetaspaceSize ) |
🔍 Comparison of Young Generation, Old Generation, and Metaspace
Feature | Young Generation | Old Generation | Metaspace (PermGen) |
---|---|---|---|
Stores | New objects | Long-lived objects | Class metadata, static variables |
GC Type | Minor GC (fast, frequent) | Major GC (slower, less frequent) | Cleaned by GC dynamically |
Lifespan | Short-lived objects | Long-lived objects | Until class unloading |
Performance | Fast allocation & deallocation | Slower (major GC is expensive) | Dynamic memory usage |
Thread Sharing | Shared by all threads | Shared by all threads | Shared by all threads |
📝 Key Takeaways
- Young Generation: Stores short-lived objects, cleared by Minor GC.
- Old Generation: Stores long-lived objects, cleaned by Major GC (Full GC).
- Metaspace: Stores class metadata, replaced PermGen in Java 8.