In depth Old gen in gc in Java
- by utk
What Is the Old Generation?
The Old Generation (Tenured Generation) is a region of the Java heap
that stores objects that have survived multiple garbage collection
cycles in the Young Generation.
Think of it as the “retirement home” for objects that the JVM
considers long-lived.
🧱 Heap Memory Layout Recap
pgsql
CopyEdit
+---------------------+--------------------+-----------------+
| Young Generation | Old Generation | Metaspace |
| (Eden + Survivors) | (Tenured) | (class metadata)|
+---------------------+--------------------+-----------------+
🔁 Object Lifecycle Through GC
1. New objects are allocated in Eden (Young Gen).
2. If they survive a few Minor GCs, they are moved to Survivor
spaces.
3. After surviving a set number of GCs (-XX:MaxTenuringThreshold,
default: 15), they are promoted to the Old Generation.
4. Once in Old Gen, they stay there until a Major (or Full) GC
removes them.
🔥 Characteristics of Old Gen
Feature Details
Long-lived objects (e.g. cached data, session
Contains
objects)
Size Larger than Young Gen
Collected by Major GC or Full GC
GC Algorithm Mark-Sweep-Compact (or Region-based in G1)
Performance
More expensive to collect, longer pause times
Impact
Risk If full → OutOfMemoryError: Java heap space
🔍 Example: Old Gen in Action
Suppose you have this:
java
CopyEdit
List<String> cache = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
cache.add("Data-" + i);
}
If cache is kept as a global variable (or static field), those strings
may survive enough Minor GCs to be promoted to Old Gen.