Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A nightly batch job processes 200 GB of records over two hours on a 16-core VM. Only total runtime matters; nobody is waiting on individual requests. Which collector would you choose, and how would you configure and validate it?

For a two-hour batch where only total runtime matters, I would start with Parallel GC, because it uses all 16 cores during short stop-the-world collections and has cheaper barriers than G1 or ZGC, so more CPU goes to the job itself. I would give it a fixed, generous heap with a large young generation so most records die in eden, leave ParallelGCThreads at its default, and judge it by GC time as a fraction of runtime. Then I would prove the choice by running the real job under Parallel and G1 and comparing wall-clock time, and look at reducing allocation in the hot loop, which often matters more than the collector.

Why this question is asked

Most GC advice is written for services where a user waits for every request. A batch job is the opposite: nobody notices a 500 ms pause, but everybody notices if the job runs past its window. Interviewers ask this scenario to see whether we pick the metric before the tool. In this answer, we will set out what matters for this job, see why Parallel GC fits it, configure the heap and threads, plan a fair comparison with G1, and look at the allocation work that usually brings the biggest gain.

1Choosing the Metric Before the Collector

A collector trades three things against each other: throughput, the share of CPU time left for the application; latency, how long a single pause lasts; and footprint, how much memory it needs. Low-pause collectors like G1 and ZGC buy shorter pauses by doing work concurrently and by adding barrier code to the application's own reference loads or stores. That work is paid in CPU.

Here, pause length is irrelevant. If the job pauses 300 ms forty times, that is 12 seconds out of 7,200, which nobody cares about as long as the total is small. The number that matters is GC time as a fraction of total runtime, and beyond that, total runtime itself. Each percent of runtime spent in GC is 72 seconds of a two-hour run.

2Why Parallel GC Fits a Throughput Job

Parallel GC, sometimes called the throughput collector, is generational like Serial but uses many threads in its stop-the-world collections. On 16 cores it copies survivors and compacts the old generation with all of them, so a pause that would take a second single-threaded takes a fraction of that. Between pauses it does nothing at all: no concurrent marking, no refinement threads, and the write barrier is a single card mark.

G1 does more work outside its pauses, through concurrent marking and remembered set refinement, and its barriers are heavier. ZGC does almost everything concurrently, with load barriers on reference reads. For services that need short pauses, that is a good trade. For our job, it is CPU taken away from processing records, and studies and benchmarks commonly show Parallel ahead of G1 by a few percent on pure throughput work. A few percent of two hours is several minutes.

Collector fit for a 2-hour batch on 16 cores
CollectorWork outside pausesFit for this job
Parallelnone; card-mark barrier onlybest first candidate
G1concurrent marking, refinement, heavier barriersa close second; measure it
Generational ZGCnearly all GC work concurrent, load barrierspays CPU for pauses we do not need
Serialnoneone thread wastes 15 cores in each pause

3Configuring the Heap and Threads

The best thing we can do for Parallel is to give it room. Most objects in a record-processing job live for one record: parse, transform, write, forget. If the young generation is large, those objects die in eden and a young collection only copies the few that survive, so it is cheap and rare. If the heap is small, collections happen constantly and objects that are still in use get promoted to the old generation, which later needs an expensive full collection.

Assume the VM has 64 GB and nothing else runs on it. A reasonable start is a fixed heap, so the JVM never spends time growing and shrinking it, with a young generation that is a large share of it.

Shell
java -XX:+UseParallelGC -Xms40g -Xmx40g -Xmn24g -XX:+AlwaysPreTouch \
-Xlog:gc*:file=gc.log:time,uptime,level,tags \
-XX:StartFlightRecording=duration=2h,filename=batch.jfr \
-jar nightly-batch.jar
// -Xms40g -Xmx40g fixed heap, no resizing during the run
// -Xmn24g large young generation: per-record objects die in eden
// -XX:+AlwaysPreTouch touch heap pages at startup instead of during the job
// ParallelGCThreads not set: on 16 CPUs the default is 8 + (16 - 8) * 5 / 8 = 13 threads

Notice what we left out. ParallelGCThreads defaults from the CPU count, all CPUs up to 8 and then five eighths of the rest, which gives 13 on 16 cores; overriding it rarely helps unless something else shares the machine. Parallel also has adaptive sizing, which tunes generation sizes toward its goals; setting -Xmn fixes the young size, which is fine for a job whose allocation pattern we know, but we can instead leave sizing adaptive and only set the heap. Either way, the GC log tells us whether we were right.

4Validating Against G1 on Real Data

Assumptions about collectors are often wrong for a specific workload, so the decision should come from a measurement. We run the same job, on the same VM type, with the same input, which should be a realistic full-size data set, not a 1 GB sample, because the live set and promotion pattern change with size.

For each run we record total wall-clock time, total GC time from the log, the number and length of full collections, and average CPU utilisation. We run each configuration at least twice, because disk and network noise can easily move a two-hour job by a minute or two. If Parallel finishes in 1 hour 52 minutes with 2 percent GC time and G1 finishes in 1 hour 58 minutes with 4 percent, the decision is easy. If they are within noise, we can pick G1 for consistency with the rest of the fleet.

5The Bigger Win: Allocating Less

The collector only cleans up what the code allocates. A hot loop that allocates 2 GB per second makes even the best collector run often. A JFR recording with allocation profiling shows which methods allocate the most, and the fixes are usually simple.

Java
// Before: boxing and a new buffer for every record
Map<String, Long> totals = new HashMap<>();
for (Record r : records) {
byte[] buf = new byte[64 * 1024]; // 64 KB per record, garbage at once
int n = r.serializeInto(buf);
totals.merge(r.key(), (long) n, Long::sum); // boxes a Long for every record
}
// After: one reused buffer and a primitive accumulator
byte[] buf = new byte[64 * 1024]; // allocated once per worker thread
Map<String, long[]> totals2 = new HashMap<>();
for (Record r : records) {
int n = r.serializeInto(buf);
totals2.computeIfAbsent(r.key(), k -> new long[1])[0] += n; // no boxing per record
}

Changes like these can halve the allocation rate, which halves the number of young collections, and they help under any collector. That is why I would do this profiling in parallel with the collector comparison, not after it.

6Follow-up questions

What does GCTimeRatio control in Parallel GC?

GCTimeRatio sets the throughput goal for Parallel's adaptive sizing. The target is that GC takes at most 1 / (1 + N) of total time, and the default N is 99, so the goal is at most 1 percent of time in GC. If GC takes more, adaptive sizing grows the generations to collect less often. Setting -XX:GCTimeRatio=19 relaxes the goal to 5 percent, which lets the heap stay smaller. It only steers sizing; it cannot make GC faster than the hardware allows.

Would ZGC ever make sense for a batch job?

Rarely. ZGC uses extra CPU and memory headroom to keep pauses short, and this job does not benefit from short pauses. One exception is a batch that shares a JVM with an online component, such as a job that also serves a status API with a latency target. Then the latency requirement returns and the trade-off changes.

7Conclusion

In this answer, we saw that a batch job where only total runtime matters should be judged by GC time as a share of runtime, not by pause length. Parallel GC fits because it uses all cores in short stop-the-world pauses and adds almost no work between them. We gave it a fixed, generous heap with a large young generation, left ParallelGCThreads at its default of 13 on 16 cores, and planned to confirm the choice against G1 on a realistic data set. Finally, we saw that reducing allocation in the hot loop often matters more than the collector.

What to say in the interviewAbout 80 seconds aloud

Open with

Since nobody waits on individual requests, I do not care about pause length, only about GC time as a fraction of the two-hour run. That points to Parallel GC.

Then, if they want more

It uses all 16 cores during stop-the-world collections, so pauses are short anyway, and between pauses it does no concurrent work and has only a card-marking write barrier, so more CPU goes to processing records than with G1 or ZGC.

I would give it a fixed heap with -Xms equal to -Xmx, sized generously for the VM, and a large young generation so that per-record objects die in eden and are never copied. I would leave ParallelGCThreads at its default, which is 13 on 16 cores.

Then I would validate rather than assume: run the full realistic data set under Parallel and under G1, at least twice each, and compare total runtime, total GC time from the unified GC log and CPU utilisation. If Parallel is clearly faster I keep it; if they are within noise I might choose G1 for fleet consistency.

At the same time I would take a JFR allocation profile of the hot loop, because reusing buffers and avoiding boxing usually cuts GC work more than any collector change.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A nightly batch job processes 200 GB of records over two hours on a 16-core VM. Only...": https://scrivio-interview-library.netlify.app/java/garbage-collection/51562b0655/