skip to content

Why can the same int-indexed hot loop stall time-to-safepoint for hundreds of milliseconds on JDK 25 under the Parallel collector but not under G1, and how do you fix it?

level: seniorimportance: should knowfreq 30%

answer

  1. collector ergonomics set a C2 flag
  2. UseCountedLoopSafepoints
  3. strip mining, a thousand iterations
  4. one flag alone polls every iteration
  5. prove it with reaching times

basics

~20 s

C2 drops the safepoint poll from int-counted loops unless UseCountedLoopSafepoints is on; G1, ZGC and Shenandoah turn it on with LoopStripMiningIter 1000, while Serial and Parallel leave it off, so a long loop polls only at its exit.

solid answer

~40 s

In JDK 25, `UseCountedLoopSafepoints` defaults to `false` in the C2 flags, and C2 then compiles a counted loop, an `int` index with a fixed stride and limit, without a poll on its back edge. G1, ZGC, Shenandoah and Epsilon set it to `true` at start-up and set `LoopStripMiningIter` to 1000, so the loop is strip-mined: an inner run of up to 1000 iterations without a poll inside an outer loop that polls. Serial and Parallel set neither, so a loop over a large array reaches no poll until it ends. I confirm it from a `--ttsp` profile showing the loop and from `jcmd <pid> VM.flags -all`. Then I either restructure the code or start Parallel with both `-XX:+UseCountedLoopSafepoints` and `-XX:LoopStripMiningIter=1000`, and compare reaching times before and after.

code

bash · 5 lines
bash
# confirm what this JVM actually runs with
jcmd <pid> VM.flags -all | grep -E 'Use(Parallel|G1)GC|UseCountedLoopSafepoints|LoopStripMiningIter'

# opt the Parallel collector into bounded loop polling (restart required)
java -XX:+UseParallelGC -XX:+UseCountedLoopSafepoints -XX:LoopStripMiningIter=1000 -jar app.jar

go deeper

for a junior

Know that a very long loop in hot code can delay every other thread when the JVM needs to stop them all.

for a middle

Explain what a counted loop is, why C2 may omit its safepoint poll, and what strip mining with a 1000-iteration inner loop changes.

for a senior

Diagnose it from evidence: one late thread, a --ttsp profile in a tight loop, and flags showing Parallel without counted-loop polls; then fix with both flags or chunked code and compare reaching times.

for a principal

Decide per service whether throughput or bounded stops matter more, and make the collector's effect on loop polling part of any collector change review.

## The mechanism in one paragraph HotSpot's optimizing JIT compiler, **C2**, recognizes **counted loops**: loops with an `int` induction variable, a constant stride and a loop-invariant limit, where the trip count is known on entry. Polling for a safepoint on every back edge of such a loop costs throughput, so by default C2 leaves the poll out, on the theory that the loop will end soon. When the loop runs over hundreds of millions of elements, "soon" becomes hundreds of milliseconds, and every other thread waits at the safepoint for it. In the safepoint log this looks like a long `Reaching safepoint` phase with one runnable thread. ## What JDK 25 actually sets, per collector The global default lives in C2's flags: `UseCountedLoopSafepoints` is `false` and `LoopStripMiningIter` is `0`. Each collector's argument processing may then override them, but only if you left them at their defaults: | Collector | `UseCountedLoopSafepoints` | `LoopStripMiningIter` | Counted loop behaviour | |---|---|---|---| | G1 | set to `true` | set to `1000` | Strip-mined: polls at least every 1000 iterations | | ZGC | set to `true` | set to `1000` | Strip-mined | | Shenandoah | set to `true` | set to `1000` | Strip-mined | | Epsilon | set to `true` | set to `1000` | Strip-mined | | Parallel | left `false` | left `0` | No poll until the loop exits | | Serial | left `false` | left `0` | No poll until the loop exits | **Loop strip mining** splits the loop into an inner loop of at most `LoopStripMiningIter` iterations with no poll, and an outer loop that polls once per inner run. Most of the unpolled loop's throughput is kept and the gap between polls is bounded. A related flag, `LoopStripMiningIterShortLoop`, defaults to a tenth of `LoopStripMiningIter`, so loops with fewer iterations than that are not strip-mined. The same source means a service moved from G1 to Parallel for throughput can gain a time-to-safepoint problem it never had, with no code change. ## Proving it is this, not something else 1. The safepoint log shows long `Reaching safepoint` phases with a small runnable count, across different operation names. 2. `-XX:+SafepointTimeout` with a `SafepointTimeoutDelay` below the stall names the same thread or thread pool each time. 3. An async-profiler `--ttsp` profile in CPU mode shows that thread inside one compiled method with a tight loop. 4. `jcmd <pid> VM.flags -all` shows the collector and `UseCountedLoopSafepoints` as `false`. If the profile is empty during long reaching phases instead, the thread was not on CPU, and counted loops are the wrong suspect. ## The fixes and what each costs - **Turn polling on for this JVM.** On Parallel, start with `-XX:+UseCountedLoopSafepoints -XX:LoopStripMiningIter=1000`, which is what G1 gets ergonomically. Set **both**: if only `UseCountedLoopSafepoints` is set, a flag constraint resets `LoopStripMiningIter` from 0 to 1, a poll on every iteration, which costs far more throughput in hot loops. - **Change the code.** Hand the work out as bounded pieces, for example separate tasks of 64 K elements each, so no single unpolled loop runs for long and the thread passes through ordinary, polled code between pieces. - **Change the collector.** Moving to G1 brings strip mining with it, but changes pause behaviour, footprint and throughput too, which is a collector decision rather than a safepoint fix. - **Do nothing** if the loop runs only in a batch path whose stalls nobody waits on. Whatever you change, compare `Reaching safepoint` for the same operations over the same traffic before and after. ```java // Once C2 compiles it under Parallel on JDK 25, this counted loop has no // safepoint poll by default: a stop requested mid-loop waits for it to end. static long checksum(int[] data) { long sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i] * 31L; } return sum; } ```

  • Why would a team leave counted-loop polls off on Parallel instead of turning them on everywhere?
    Because Parallel is chosen for throughput, and bounded polls cost a little of it in the hottest loops. If the service is a batch job whose stops nobody waits on, long time-to-safepoint is harmless. The flag earns its cost only where a stop delays requests, and that is measured from the safepoint log, not assumed.
  • What happens on JDK 25 if you set -XX:+UseCountedLoopSafepoints alone under Parallel?
    A flag constraint sees counted-loop safepoints enabled with `LoopStripMiningIter` still 0, prints that it must be at least 1, and sets it to 1. Every iteration then polls, which bounds time-to-safepoint but costs more throughput than the 1000-iteration strip mining G1 gets. Setting `-XX:LoopStripMiningIter=1000` as well avoids that.

  • Every JIT-compiled loop polls for a safepoint on each back edge
  • UseCountedLoopSafepoints is on by default for every JDK 25 collector
  • -XX:+UseCountedLoopSafepoints alone gives Parallel the same polling as G1
  • Changing the collector cannot change how compiled loops reach safepoints
  • A long reaching phase always means the operation itself is slow

Assumes JDK 25 with C2: the flag defaults and the per-collector overrides are those in jdk-25.0.4's C2 flags and collector argument code.