Video in progress
What are the write barrier and the load barrier, which GC uses which, and how does this affect throughput?
A barrier is a small piece of code the JIT places around every reference write or read, to tell the GC what the application is doing. Parallel and G1 use a write barrier on reference writes; ZGC relies mainly on a load barrier, and Shenandoah on both. The heavier the barrier, the more work the GC can do concurrently, but the slower the application code gets.
📖 Answer
order.customer = c is not just a single mov instruction. Depending on the GC, there is extra code around it.
Parallel/Serial: the lightest post-write barrier: mark the matching byte in the card table as "dirty". A young GC finds old→young references this way.
G1: two write barriers:
- pre-write (SATB): during concurrent marking it records the old value, so that marking doesn't "lose" anything;
- post-write: if the reference crosses regions, it marks the card and adds it to the refinement queue. That is why a reference write in G1 is noticeably more expensive than in Parallel.
ZGC: a load barrier on every reference read from the heap: it checks whether the reference's "color" is right and, if needed, finds the object's new address and fixes the reference. In generational ZGC, store barriers were added too. The fast path is one or two instructions, but reads are far more common than writes.
Shenandoah: a load-reference barrier (to find the relocated object) + a SATB write barrier.
The effect: in CPU-heavy code that reads many references, throughput usually drops by a few percent up to ten percent in the order Parallel → G1 → ZGC, in exchange for shorter pauses. The exact number depends on the load, and you can't say without measuring.
One practical consequence: a big mutable graph (for example, references in a cache that is constantly updated) keeps G1's refinement threads busy. If the Refine time grows in JFR and the GC log, that is the reason.
Sign up to mark studied questions and save your progress.