Hard👤 8-15 years 2 min read

Choosing a GC: Throughput vs Latency — Interview Questions

Asked inAmazonNetflixGoogleGoldman Sachs
#garbage collection#g1#zgc#shenandoah#latency#throughput
Report issue

⚡ Short Answer

It's a throughput-vs-latency trade-off. Parallel GC maximises throughput but has long stop-the-world pauses — good for batch. G1 (the default) balances the two with region-based, mostly-concurrent collection and a pause-time target — good for general server apps. ZGC and Shenandoah are concurrent, region-based collectors that keep pauses sub-millisecond even on multi-hundred-GB heaps — pick them for latency-sensitive services with large heaps, at a small throughput cost.

Coffee Chat Question

Concept Made Simple

How do the modern collectors (Parallel, G1, ZGC, Shenandoah) trade off throughput vs latency, and how do you choose?

🧠Mind Map Answer

Remember It Faster

There's no 'best' collector — there's the right one for your SLO. Every collector trades some combination of pause time, throughput, footprint, and heap-size ceiling.

ParallelMax throughput, long STW — batch/ETL
G1 (default)Balanced, pause-target, region-based
ZGC / ShenandoahSub-ms pauses, huge heaps, concurrent
SerialTiny heaps / single-core / containers

Rule of thumb: batch → Parallel, typical service → G1, low-latency / very large heap → ZGC or Shenandoah, tiny footprint → Serial. Measure allocation rate and pause SLO before switching.

Key takeaway: choose by SLO, then tune. Reach for the deep-dive pages (G1 internals, ZGC) once you've picked the family that matches your latency budget.

⌨️Hands-on Keyboard

Learn by Doing

bash
# Batch job: favour throughput
java -XX:+UseParallelGC -Xmx8g BatchJob

# Latency-sensitive service on a large heap
java -XX:+UseZGC -Xmx64g -XX:+ZGenerational Service

# G1 (default) with an explicit pause target
java -XX:MaxGCPauseMillis=100 -Xmx8g Service

🔥What If?

Think Beyond the Expected

A trading service has a 200GB heap and a 1ms pause SLO — which collector, and why not G1?

ZGC or Shenandoah. G1's stop-the-world pauses grow with heap and live-set size, so at 200GB it can't hold a 1ms SLO. ZGC/Shenandoah do their marking, relocation, and reference processing concurrently with the application using load/read barriers, keeping pauses sub-millisecond almost independently of heap size — the exact profile a large, latency-critical service needs. You accept a modest throughput/CPU overhead for that predictability.

😂Real World

Nightly analytics or Spark jobs run Parallel GC to finish faster; a typical Spring Boot API runs the G1 default and rarely needs to change; latency-critical trading, ad-serving, and gaming backends on big heaps move to ZGC/Shenandoah specifically to kill tail-latency pauses that show up as p99 spikes.

🎯Interviewer's Expectation

Keywords they're listening for:

throughput vs latency trade-offParallel = batchG1 = balanced defaultZGC/Shenandoah = sub-ms, large heapchoose by SLO then tune

⚠️Common Mistakes

  • Assuming one collector is universally fastest
  • Using G1 for a very large heap with tight pause SLOs
  • Switching collectors without measuring allocation rate/pauses

Best Practices

  • Pick the collector family from your latency SLO
  • Right-size the heap before blaming the collector
  • Verify with GC logs / JFR after any change

🔁Follow-up Questions

  • 1How does G1 achieve its pause target internally?
  • 2How do ZGC/Shenandoah stay concurrent (barriers)?
  • 3How do you measure whether GC is actually your bottleneck?

🧩Related Technologies

G1ZGCShenandoahJFR

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: Garbage Collection (Advanced Java)
Interview question: "How do the modern collectors (Parallel, G1, ZGC, Shenandoah) trade off throughput vs latency, and how do you choose?"

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions