JVM.MemoryManagement.What are the differences between Young Generation, Old Generation, and Permanent Generation (or Metaspace)?

The JVM Heap is divided into different memory areas to manage object lifecycles efficiently. The main divisions are:

  1. Young Generation – Short-lived objects
  2. Old Generation – Long-lived objects
  3. 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

FeaturePermGen (Before Java 8)Metaspace (Java 8+)
StorageFixed-size (can run out of space)Dynamically growing
Garbage CollectionGC cleans it but can cause OOM errorsDynamically managed
Configurable?Yes (-XX:MaxPermSize)Yes (-XX:MaxMetaspaceSize)

🔍 Comparison of Young Generation, Old Generation, and Metaspace

FeatureYoung GenerationOld GenerationMetaspace (PermGen)
StoresNew objectsLong-lived objectsClass metadata, static variables
GC TypeMinor GC (fast, frequent)Major GC (slower, less frequent)Cleaned by GC dynamically
LifespanShort-lived objectsLong-lived objectsUntil class unloading
PerformanceFast allocation & deallocationSlower (major GC is expensive)Dynamic memory usage
Thread SharingShared by all threadsShared by all threadsShared 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.
This entry was posted in Без рубрики. Bookmark the permalink.