MediumExperience level: 3-5 years · 8-15 years1 min read

How do you choose a garbage collector — Serial, Parallel, G1, ZGC or Shenandoah?

Reviewed by Gurusankar M.

Asked inAmazonGoogleMicrosoft
#g1#parallel gc#zgc#shenandoah#gc selection#latency
Report issue

Short Answer

Parallel GC = max throughput, batch jobs (tolerates pauses). G1 = balanced default for most services (predictable pauses). ZGC/Shenandoah = very large heaps needing sub-millisecond pauses. Serial = tiny heaps/single core. Match the collector to your throughput-vs-latency goal.

On this page

Concept Made Simple

Coffee Chat Question

“How do you choose a garbage collector — Serial, Parallel, G1, ZGC or Shenandoah?”

Remember It Faster

Mind Map Answer

Parallel
throughput, batch — big pauses OK
G1 (default)
balanced, pause target -XX:MaxGCPauseMillis
ZGC / Shenandoah
huge heaps, ~sub-ms pauses
Serial
small heap, single core

Think Beyond the Expected

What If?

A low-latency API on a 64 GB heap has unacceptable G1 pauses — what do you try?

Switch to ZGC (or Shenandoah): concurrent collectors that keep pause times in the sub-millisecond range largely independent of heap size, trading some throughput/CPU. Then measure p99 latency to confirm the improvement.

Real World

Most services run G1 (the default since Java 9). Latency-critical, large-heap systems move to ZGC; throughput-bound batch jobs sometimes prefer Parallel. The choice is a deliberate latency/throughput trade-off.

Interviewer's Expectation

Keywords they're listening for:

  • throughput vs latency
  • G1 default + pause target
  • ZGC/Shenandoah for low pause/large heap
  • Parallel for batch
  • measure to decide

Common Mistakes

  • Switching GCs without measuring
  • Using Parallel for latency-sensitive APIs
  • Assuming a GC change is free of CPU cost

Best Practices

  • Start with G1; change only with data
  • Pick by latency vs throughput goal
  • Validate p99 latency, not just averages

Follow-up Questions

  1. What does -XX:MaxGCPauseMillis actually do in G1?
  2. What does ZGC trade for its low pauses?
  3. How do you A/B test a GC change safely?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: GC Tuning (JVM)
Interview question: "How do you choose a garbage collector — Serial, Parallel, G1, ZGC or Shenandoah?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.