Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 2 OF 4 · 2 MIN READ

Garbage Collection and Collectors (G1, ZGC)

Garbage collection frees memory of objects that are no longer reachable. Interviewers ask how the JVM decides what is garbage, how generational collection works, and how G1, ZGC and Parallel GC differ.

What you will learn

  • Explain reachability from GC roots
  • Explain the generational hypothesis, minor and major GC
  • Compare Serial, Parallel, G1, ZGC and Shenandoah
  • Read basic GC logs

Key concepts

GC roots

Starting points: local variables on thread stacks, static fields, active threads and JNI references. Anything not reachable from a root is garbage, even if objects reference each other in a cycle.

Generational hypothesis

Most objects die young. New objects go to eden; a minor GC copies survivors between survivor spaces, and objects that survive several cycles are promoted to the old generation.

Stop-the-world

Pauses where application threads stop so the collector can work safely. Modern collectors do most work concurrently to keep pauses short.

Collectors

Serial (single thread, small apps), Parallel (throughput, batch jobs), G1 (default since Java 9, region-based with pause targets), ZGC (generational since Java 21, sub-millisecond pauses on large heaps), Shenandoah (low pause, concurrent compaction).

Step by step

Choosing a collector

  • Most services: G1 (default), tune -XX:MaxGCPauseMillis if needed
  • Large heaps or strict latency: ZGC (-XX:+UseZGC)
  • Batch jobs that care only about throughput: Parallel
  • Tiny containers with one CPU: Serial is often picked automatically

Examples

GC logging and quick checks

# unified GC logging (Java 9+)
java -Xlog:gc*:file=gc.log:time,uptime,level,tags -jar app.jar

# live view of GC activity
jstat -gcutil <pid> 1000

# what is using the heap right now
jcmd <pid> GC.class_histogram | head -20

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Allocate many short-lived objects and watch minor GCs in the log.
  • Explain why calling System.gc() in application code is a bad idea.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Can Java have memory leaks if it has a garbage collector?

Yes. The GC only frees unreachable objects. If your code keeps references you no longer need, such as an ever-growing static map, listeners never removed or ThreadLocals in pooled threads, those objects stay alive forever.

What are strong, soft, weak and phantom references?

Strong: normal, never collected while reachable. Soft: collected only when memory is low (memory-sensitive caches). Weak: collected at the next GC once only weakly reachable (WeakHashMap keys). Phantom: for cleanup after collection (Cleaner).

Summary

The GC frees objects unreachable from roots, using generations because most objects die young. G1 is the sensible default, ZGC for low latency on big heaps, and leaks happen when you keep references you no longer need.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus