skip to content

Safepoint Stall Diagnosis

Finding the stop the collector did not cause: reading safepoint logs, naming the VM operation, and catching the thread that was slow to arrive. Interviewers ask who stopped the app.

part ofJVM internalsread it →
on this pageshow

questions

5

In the safepoint log a JDK 25 HotSpot JVM prints with -Xlog:safepoint, what do the Reaching safepoint, At safepoint and Threads fields each tell you?

level: middleimportance: should knowfreq 40%

answer

  1. one line per stop
  2. name of the VM operation
  3. waiting versus working
  4. nanoseconds, three phases summed
  5. threads still running at the request

basics

~20 s

Each line names one VM operation and splits its stop into nanosecond phases: Reaching safepoint is the wait for every thread to stop, At safepoint is the operation's own work, and Threads counts the threads it had to wait for.

solid answer

~40 s

With `-Xlog:safepoint`, JDK 25 prints one line per safepoint: `Safepoint "<operation>"`, then `Time since last`, `Reaching safepoint`, `At safepoint`, `Leaving safepoint` and `Total`, all in nanoseconds, then `Threads: R runnable, T total`. `Reaching safepoint` is time-to-safepoint, from the request until every Java thread is safe; `At safepoint` is the VM operation's own work; `Leaving safepoint` is the restart; `Total` is their sum. `T` is the number of Java threads, and `R` is how many were still running Java or VM code when the VM thread first checked, so they had to be waited for. A large `Reaching` with a small `R` points at one late thread; a large `At` points at the operation itself.

code

console · 1 line
console
[812.044s][info][safepoint] Safepoint "PrintThreads", Time since last: 9874120332 ns, Reaching safepoint: 386201554 ns, At safepoint: 41022871 ns, Leaving safepoint: 70114 ns, Total: 427294539 ns, Threads: 1 runnable, 310 total

go deeper

for a junior

Recall that the JVM can log every stop of all threads with -Xlog:safepoint, and that each line names the operation that caused it.

for a middle

Explain the phases: reaching is waiting for threads, at is the operation's work, leaving is the restart, and total is their sum in nanoseconds.

for a senior

Read the split to decide the next step: a long reaching phase with one runnable thread means hunt the late thread; a long at phase means find who requested the operation.

for a principal

Treat the safepoint log as cheap standing evidence and insist that pause analysis sums stops by operation name rather than trusting GC pause totals alone.

## What the safepoint log records A **VM operation** is a piece of work that HotSpot's VM thread performs on behalf of the runtime: a collection, a thread dump, a class redefinition, a heap dump. Most of them must run while every Java thread is halted at a **safepoint**, a point where the thread's state is known to the runtime. The **safepoint log** prints one line for every such stop, whoever asked for it. That makes it the evidence for a stop the garbage-collection log does not explain. On JDK 25 you turn it on with unified logging at the `info` level of the `safepoint` tag: ```bash java -Xlog:safepoint:file=/var/log/app/safepoint.log:time,uptime,level,tags:filecount=5,filesize=20m -jar app.jar ``` One line, with made-up numbers but the fields and order the JDK 25 source prints: ```console [3605.112s][info][safepoint] Safepoint "ParallelCollectForAllocation", Time since last: 2381194021 ns, Reaching safepoint: 412883 ns, At safepoint: 18237710 ns, Leaving safepoint: 61204 ns, Total: 18711797 ns, Threads: 4 runnable, 96 total ``` ## The fields, one by one | Field | Measured from and to | What it answers | |---|---|---| | `Safepoint "<name>"` | none | Which VM operation asked for the stop | | `Time since last` | end of the previous safepoint to the start of this one | How long the application ran between stops | | `Reaching safepoint` | the request to the moment every Java thread is safe | **Time-to-safepoint**: the wait for latecomers | | `At safepoint` | all threads safe to the operation finishing | The operation's own cost | | `Leaving safepoint` | operation finished to threads released | Restart cost, normally small | | `Total` | the request to the release | The sum of the three phases | | `Threads: R runnable, T total` | taken at the first check | `T` Java threads, `R` of them still running when checked | Points that trip people up: - **All durations are nanoseconds**, not milliseconds. `412883 ns` is 0.4 ms. - **`Total` equals `Reaching` plus `At` plus `Leaving`.** A thread that stopped at once was frozen for close to `Total`; the thread that arrived last ran for most of `Reaching` and was frozen only for the rest. - **The operation name is the requester's name**, not a category. On the Parallel collector the collection operations include `ParallelCollectForAllocation` and `ParallelGCCollect`; on G1 they include `G1CollectForAllocation` and `G1PauseRemark`. Anything else, such as `PrintThreads` or `RedefineClasses`, is not a collection. ## What "runnable" means here The `runnable` count is not the number of `RUNNABLE` threads in a thread dump. When the VM thread arms a safepoint it examines every Java thread once. A thread that is **blocked** (waiting on a lock, parked, sleeping) or **in native code** with a walkable stack is already safe and is not counted. The rest, threads executing Java code or inside the VM, are counted as runnable: they are the ones the VM thread must now wait for. 1. A large `Reaching safepoint` with `1 runnable` says one thread was slow to arrive. The next step is to find that thread. 2. A large `Reaching safepoint` with many runnable threads is ambiguous: one of them may still be the latecomer, or several may have been slow together, as when the process is starved of CPU, for example under container CPU throttling. 3. A large `At safepoint` with a small `Reaching` says the operation itself was expensive, such as a heap dump of a large heap. A thread inside a native call can show `RUNNABLE` in a thread dump and still be safe for a safepoint, which is why the two counts differ. ## Companion output - `-Xlog:safepoint+stats` adds a table row per safepoint, with the columns `VM Operation`, `[ threads: total initial_running ]`, `[ time: sync vmop total ]` and `page_trap_count`, and at exit a count of safepoints per operation plus the maximum sync time and the maximum operation time. - A **thread-local handshake** does not stop every thread and does not print a safepoint line at all; `-Xlog:handshake` logs those. - With `-XX:+SafepointTimeout`, a stop whose reaching phase exceeds `SafepointTimeoutDelay` also prints, at warning level, the threads that did not reach the safepoint. ## Reading a window, not a line One line is an anecdote. Sum `Total` by operation name over an hour, then split the heaviest names by `Reaching` against `At`. That tells you whether to chase a requester (a big `At`), a late thread (a big `Reaching`), or nothing at all.

  • Why can Total be large on a line whose At safepoint value is tiny?
    Because `Total` includes `Reaching safepoint`. If one thread took 300 ms to arrive, every thread that stopped at once waited those 300 ms even though the operation then ran for a millisecond. The cost is time-to-safepoint, so the fix lies with the late thread, not with the operation that requested the stop.
  • How would you get a per-operation summary for a whole run instead of reading individual lines?
    Add `-Xlog:safepoint+stats`. It prints a table row per safepoint with sync, operation and total times, and at JVM exit a count of safepoints per operation name with the maximum sync time and maximum operation time. For a running service, summing `Total` by operation name from the rotated `-Xlog:safepoint` files gives the same picture.

A meeting that cannot start until the last invitee walks in: Reaching safepoint is the time spent waiting at the door, At safepoint is the meeting itself, and the runnable count is how many invitees were still on their way when the chair called it.

  • Reaching safepoint is the collector's own pause work
  • The runnable count equals the RUNNABLE threads in a thread dump
  • Every line in the safepoint log is a garbage collection
  • The safepoint log's durations are in milliseconds
  • A thread-local handshake prints its own safepoint line

Assumes JDK 25: the field set and order are those the jdk-25.0.4 source prints, and the safepoint log layout has changed across releases, so check the release before parsing an older log.

open as a page

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%

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.

open as a page

A JDK 25 HotSpot safepoint log shows Reaching safepoint near 400 ms with one runnable thread; how do you identify that late thread and the code it was running?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Name the thread with -XX:+SafepointTimeout and a SafepointTimeoutDelay below the stall, which logs the threads that had not arrived; then find its code with an async-profiler --ttsp profile, which samples only between the safepoint request and synchronization.

open as a page

On a JDK 25 HotSpot service using the Parallel collector, stopped time exceeds GC pause time and -Xlog:safepoint names operations that are not collections; how do you find who stopped the application?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Group the safepoint log by operation name, keep the non-collection names, map each to its requester (PrintThreads to a thread dump, GC_HeapInspection to a class histogram, RedefineClasses to an agent), then correlate timestamps to find the caller.

open as a page

For a fleet of latency-sensitive JDK 25 services, what policy would you set for diagnostic tooling that stops the world, such as scheduled thread dumps, class histograms and agents?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

Measure each tool's stopped time from the safepoint log by operation name, then set rules by cost: frequent stops get cheaper alternatives or lower frequency, huge ones run only on instances out of rotation, and agents load at start-up.

open as a page