Video in progress
When do you consider Parallel GC a better choice than G1?
For work where pause length doesn't matter but total throughput does: batch jobs, ETL, report generation. Parallel GC spends no CPU on concurrent work and its barriers are lightweight, so it gets more work done in the same time.
📖 Answer
Parallel GC (-XX:+UseParallelGC) collects both young and old in stop-the-world mode, but with many threads. Its goal is to give the largest possible share of time to application code.
G1 pays a price to keep pauses short:
- part of the marking runs alongside the application and shares the CPU;
- a write barrier runs on every reference write;
- remembered sets take extra memory.
An example: a job that runs at night, reading 40 million rows, calculating and writing to a file. A 300 ms pause makes no difference to it at all, but finishing 10% sooner matters. Here Parallel GC usually wins.
On the other hand, for a REST service answering user requests, a Full GC pause of several seconds on a big heap with Parallel GC is unacceptable.
java -XX:+UseParallelGC -Xmx8g -jar report-job.jar
Before deciding I still run both on real data and compare total time and the GC log. On a small heap the difference is often barely visible.
Sign up to mark studied questions and save your progress.