Java Developer Interview Question

How do you choose a garbage collector, and what do you know about ZGC?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

G1 is the default and suits most services: region based, generational, with a configurable pause target in the tens of milliseconds. ZGC is the choice when pause time dominates, since it does almost all work concurrently and keeps pauses under a millisecond even on very large heaps; it has been generational by default since Java 23. Parallel GC still wins on raw throughput for batch jobs where pauses do not matter.

Why interviewers ask this

The interviewer wants to know whether you can reason about a latency versus throughput tradeoff rather than copying flags from a blog post. Naming the current defaults shows your knowledge is fresh. It also opens the more important question of whether you would tune the collector at all, since most garbage collection problems are really allocation rate or heap sizing problems in the application.

How to structure your answer

  • Frame it as a latency versus throughput and footprint tradeoff.
  • Give the default and when it is fine to leave it alone.
  • Say when ZGC earns its cost, and what that cost is.
  • Insist on measuring before tuning any flags.

Example answer

Spoken example, first person

I start by leaving it alone, because G1 with a sensible heap size handles most services, and it already balances throughput against a pause goal you can set. I look at the garbage collection logs before touching anything: allocation rate, pause distribution, how much is promoted, and whether the heap is simply too small. Most of the time what looks like a collector problem is an application allocating far too much, or a heap sized so tightly that it is collecting constantly. If pauses genuinely drive my tail latency, for example a trading or bidding service with a large heap, I move to ZGC, which does marking and relocation concurrently with load barriers and keeps pauses well under a millisecond. It has been generational since Java 23, which fixed the throughput penalty that made people avoid it. The tradeoff is extra CPU and memory overhead for those barriers. For a nightly batch job I go the other way and use Parallel, since a two second pause is irrelevant and raw throughput is what I want.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What would you look at in a garbage collection log first?
  • What is a humongous allocation in G1, and why does it hurt?
  • How does heap size affect pause times and collection frequency?

Related java developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot