Topic 23 of 30 · Expert
How would you investigate rising JVM latency and old-generation usage?
Correlate p95/p99 latency with GC pauses, allocation rates and heap occupancy after full collections. High allocation pressure and retained objects are different problems.
Java 17+; compile each complete program as Main.java.
Detailed answer
Correlate p95/p99 latency with GC pauses, allocation rates and heap occupancy after full collections. High allocation pressure and retained objects are different problems.
Capture bounded profiling evidence with JFR, GC logs and heap analysis in a safe environment. Inspect dominator trees for unexpectedly retained graphs instead of increasing heap size blindly.
Select a collector and heap configuration based on pause goals, throughput and available CPU. Container limits, direct buffers, thread stacks and metaspace can exhaust memory outside the Java heap.
Internal working
Collectors trace live graphs from GC roots. A reachable object is live even if business logic no longer needs it; tuning cannot reclaim an unbounded live cache.
Practical scenario
A service stores every request result in an unbounded map. Heap growth persists after collections and latency worsens as collection work grows.
class Main {public static void main(String[] a){Runtime r=Runtime.getRuntime();System.out.println("max heap="+r.maxMemory());System.out.println("used estimate="+(r.totalMemory()-r.freeMemory()));}}Common mistakes
- Treating max heap as total process memory.
- Tuning GC before examining retention.
Key takeaways
- Correlate measurements.
- Inspect retained graphs.
- Budget memory outside the heap.
Follow-up challenges
- When would a heap dump be too risky?
- How do direct buffers affect container memory?