Garbage CollectionIntermediate
1Question 1 of 2
bashHow does Garbage Collection operate in modern Java (G1GC and ZGC)?
Explanation & Concept
Java uses generational garbage collection based on the **Weak Generational Hypothesis**: most objects die shortly after creation.
• G1GC (Garbage-First): Divides the heap into equal-sized regions, prioritizing regions with the most garbage to meet user-defined pause targets (`-XX:MaxGCPauseMillis`). • ZGC (Z Garbage Collector): A scalable low-latency GC capable of handling terabyte heaps with sub-millisecond maximum pause times by performing all object relocation concurrently with running application threads.
Code Example
bash
# Launch Java application with ZGC for low-latency microservices
java -XX:+UseZGC -XX:+ZGenerational -Xmx8g -jar app.jarHow It Works & Key Takeaways
Generational ZGC keeps GC pause times under 1 millisecond regardless of whether the heap is 1GB or 16TB.