0Pricing
Java Academy · Lesson

JVM Heap Regions and Object Lifecycle

Explore Young Generation (Eden, Survivor), Old Generation, and Metaspace and how objects move between them.

JVM Heap Regions and Object Lifecycle is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The JVM Memory Model

The JVM divides memory into several areas. The heap holds all class instances and arrays. Off-heap areas include Metaspace (class metadata), stack frames, and native memory.

Young Generation: Eden Space

Newly allocated objects go to Eden. When Eden fills up, a Minor GC runs. Surviving objects are copied to a Survivor space.

// Object allocation starts in Eden:
String s = new String("hello");  // allocated in Eden
List<Integer> list = new ArrayList<>();  // also Eden

Survivor Spaces: S0 and S1

Two Survivor spaces (S0 and S1) hold objects that survived at least one GC. Objects alternate between S0 and S1 on each Minor GC. Their age counter increments.

Tenuring Threshold

When an object's age exceeds the tenuring threshold (default 15), it is promoted to the Old Generation. Large objects may skip Young Gen and go directly to Old Gen.

// JVM flags:
// -XX:MaxTenuringThreshold=15  (default)
// -XX:PretenureSizeThreshold=1m  (objects > 1MB go directly to Old Gen)

Old Generation (Tenured Space)

Long-lived objects reside in Old Gen. Major (Full) GCs collect Old Gen and are more expensive. Minimizing long-lived object count reduces Full GC frequency.

Metaspace (Java 8+)

Metaspace replaced PermGen in Java 8. It holds class metadata, method bytecode, and interned strings. It uses native memory and grows dynamically (bounded by -XX:MaxMetaspaceSize).

// Flag examples:
// -XX:MetaspaceSize=64m        (initial Metaspace size)
// -XX:MaxMetaspaceSize=256m   (cap to avoid native OOM)

Object Allocation Fast Path

The JVM uses Thread-Local Allocation Buffers (TLABs) to allocate from Eden without synchronization. Each thread has its own small Eden slice.

Object Lifecycle: Reachability

An object is live if it is reachable from a GC root (stack variable, static field, JNI reference). Unreachable objects are candidates for collection.

Object a = new Object(); // reachable via local variable
a = null;                 // now unreachable — eligible for GC

Soft, Weak, and Phantom References

SoftReference is cleared before OOM (used for caches). WeakReference is cleared at the next GC. PhantomReference is used for post-mortem cleanup.

Cache<String, Image> cache = new LinkedHashMap<>();
// Use SoftReference to let GC reclaim cache under memory pressure:
SoftReference<Image> ref = new SoftReference<>(loadHeavyImage());
Image img = ref.get(); // null if GC collected it

Object Finalization (Avoid)

finalize() is deprecated and unpredictable — the GC calls it before collecting, but timing is not guaranteed. Use Cleaner (Java 9+) or try-with-resources instead.

Heap Sizing Flags

Control heap with -Xms (initial) and -Xmx (maximum). Set them equal in production to avoid heap resizing pauses.

// Recommended production flags:
// -Xms512m -Xmx512m   (fix heap to 512 MB)
// -XX:NewRatio=2       (Old:Young = 2:1)

Quick Check

Where does a newly allocated object first reside in the JVM heap?

Recap

The JVM heap has Young Gen (Eden + Survivors) and Old Gen. Objects age through Minor GCs and get promoted. Metaspace holds class metadata. Control with -Xms/-Xmx and tuning flags.

Frequently asked questions

Is the “JVM Heap Regions and Object Lifecycle” lesson free?

Yes — the full text of “JVM Heap Regions and Object Lifecycle” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “JVM Heap Regions and Object Lifecycle”?

Explore Young Generation (Eden, Survivor), Old Generation, and Metaspace and how objects move between them. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “JVM Heap Regions and Object Lifecycle” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. JVM Heap Regions and Object Lifecycle
  2. GC Algorithms: Serial, G1, ZGC, Shenandoah
  3. Detecting and Fixing Memory Leaks
  4. GC Tuning Flags and JVisualVM Profiling
← Back to Java Academy