Definition
- Young Gen: short-lived, fast GC, small\n- Old Gen: long-lived, slow GC, large\n- Most objects die in Young Gen (generational hypothesis)\n- Promotion: objects surviving N GCs move to Old Gen
Understanding the Concepts
What is the difference between Young and Old Generation?
The Young Generation holds newly created objects that are expected to die quickly. The Old Generation holds objects that have survived multiple garbage collection cycles and are expected to live for a long time.
In a banking application at National Bank of India, when Rajesh processes a fund transfer, the request object, validation result, and temporary strings are all created in the Young Generation. They exist for milliseconds and die when the method returns. The database connection pool, application configuration, and cached interest rate tables live in the Old Generation because they persist for the entire application lifetime.
package com.bank.memory;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
public class GenerationDemo {
// Old Generation: lives for the entire application lifetime
private static final Map<String, BigDecimal> interestRates = new HashMap<>();
static {
interestRates.put("SAVINGS", new BigDecimal("4.5"));
interestRates.put("CURRENT", new BigDecimal("0.0"));
interestRates.put("FD", new BigDecimal("7.25"));
}
public static void main(String[] args) {
// Young Generation: born and dies within this method
String accountId = "SAV2001";
BigDecimal rate = interestRates.get("SAVINGS");
BigDecimal balance = new BigDecimal("100000.00");
BigDecimal interest = balance.multiply(rate).divide(new BigDecimal("100"));
System.out.println("Interest for " + accountId + ": " + interest);
// accountId, balance, interest are all Young Gen garbage now
}
}
Interest for SAV2001: 4500.000
Why are they separated?
The separation exploits the weak generational hypothesis: most objects die young. By collecting the Young Generation frequently (Minor GC), the JVM reclaims memory from short-lived objects without scanning the entire heap.
- Minor GC collects only the Young Generation. It is fast because the Young Gen is small and most objects are dead.
- Full GC collects the entire heap including Old Generation. It is slow because it scans all live objects.
In a banking system processing 10,000 transactions per second, each transaction creates dozens of temporary objects. Without generational collection, the GC would have to scan every object in the heap every time, which would cause unacceptable pauses.
How do objects move from Young to Old?
Objects follow this lifecycle:
- Created in Eden (part of Young Generation).
- If alive during Minor GC, copied to a Survivor space (S0 or S1).
- Each Minor GC increments the object's age counter.
- When the age reaches the tenuring threshold (default 15), the object is promoted to Old Generation.
package com.bank.memory;
public class PromotionDemo {
public static void main(String[] args) {
// This object will eventually be promoted to Old Gen
// if it survives enough Minor GC cycles
Object longLived = new Object();
// Force allocations to trigger Minor GC
for (int i = 0; i < 1_000_000; i++) {
byte[] temp = new byte[1024]; // Short-lived, dies in Young Gen
}
// longLived has survived many Minor GCs and is now in Old Gen
System.out.println("Long-lived object still reachable: " + longLived);
}
}
# Run with GC logging to observe promotions
java -Xlog:gc*:file=gc.log -Xmx256m PromotionDemo
What are the size differences?
The Old Generation is typically 2-3 times larger than the Young Generation. The default -XX:NewRatio=2 means Old Gen is twice the size of Young Gen.
| Aspect | Young Generation | Old Generation |
|--------|-----------------|----------------|
| Size | Smaller (1/3 of heap) | Larger (2/3 of heap) |
| Objects | Short-lived, temporary | Long-lived, persistent |
| GC type | Minor GC (fast) | Major/Full GC (slow) |
| Frequency | Collected often | Collected rarely |
| Algorithm | Copying collector | Mark-Sweep-Compact |
| JVM flag | -XX:NewSize, -XX:MaxNewSize | Remainder of -Xmx |
What are common mistakes?
Wrong approach:
// WRONG: caching everything makes objects long-lived unnecessarily
package com.bank.service;
import java.util.*;
public class TransactionCache {
// This unbounded cache will fill Old Gen and cause Full GC
private static final Map<String, Object> cache = new HashMap<>();
public void processTransaction(String txnId, Object data) {
cache.put(txnId, data); // Never evicted!
}
}
Correct approach:
package com.bank.service;
import java.util.*;
public class TransactionCache {
// CORRECT: bounded cache with eviction
private static final int MAX_SIZE = 10_000;
private static final Map<String, Object> cache = new LinkedHashMap<>(MAX_SIZE, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Object> eldest) {
return size() > MAX_SIZE;
}
};
public void processTransaction(String txnId, Object data) {
cache.put(txnId, data); // Old entries evicted automatically
}
}
What should a senior engineer know?
- Premature promotion happens when Survivor spaces are too small. Objects jump to Old Gen before they can die in Young Gen, causing more frequent Full GCs.
- G1 GC uses a different layout (regions instead of contiguous spaces) but still applies generational collection.
- ZGC and Shenandoah are region-based collectors that do not strictly separate generations (though ZGC added generational mode in Java 21).
- Tenuring threshold tuning. Use
-XX:MaxTenuringThresholdto control how many Minor GCs an object must survive before promotion.
What are common interview follow-up questions?
- What happens if the Old Generation fills up?
- How does the tenuring threshold affect promotion?
- What is the default Young-to-Old ratio?
- How does G1 GC handle generations differently?
- What triggers a Full GC vs a Minor GC?
Key Takeaway
Young Gen holds new short-lived objects, collected frequently and cheaply (Minor GC). Old Gen holds long-lived objects, collected rarely and expensively (Major/Full GC).\n\n
Remember one sentence: What is the difference between the Young Generation and the Old Generation memory spaces is fundamental knowledge for Java interviews and production banking work.
---