skip to content

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%

answer

  1. measure the fleet by operation name
  2. cheap and frequent versus rare and huge
  3. alternatives that need no global stop
  4. consistency is what you give up
  5. where and when, not whether

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.

solid answer

~40 s

I would start from evidence: sum `-Xlog:safepoint` `Total` by operation name across the fleet, so each tool has a measured cost. Then I would split the rules by shape. Frequent, moderate stops such as `PrintThreads` from a sidecar's `jstack` or `ThreadDump` from a metrics library get a frequency cap or a cheaper alternative: on JDK 25, `jcmd Thread.dump_to_file` snapshots each thread with a per-thread handshake rather than one global safepoint, and `ThreadMXBean.getThreadInfo` without a stack depth needs no stop at all. Rare, huge stops such as `HeapDumper` and `GC_HeapInspection` are allowed only on an instance taken out of rotation. Agents that redefine classes load at start-up, not attached at peak. The tradeoff to own: cheaper captures lose the single consistent instant a global-safepoint dump gives you.

go deeper

for a junior

Know that thread dumps, heap dumps and agents can each pause the whole service briefly, so running them is not free.

for a middle

Explain which tools map to which safepoint operation names and how to read their cost from the safepoint log.

for a senior

Show how to attribute a fleet's non-GC stopped time to specific sidecars, libraries or habits, and which cheaper capture replaces each.

for a principal

Own the tradeoff between diagnosability and pause budget: set frequency caps, where heavy captures may run and how agents load, and revisit them with measured totals.

## Why this is a policy question Every HotSpot JVM can be asked by outside tools and in-process libraries to run **VM operations** that halt all Java threads at a **global safepoint**: thread dumps, class histograms, heap dumps, class redefinition. Each one is legitimate on its own. Across a fleet, they become a standing cost nobody owns: a health-check sidecar running `jstack` every 30 seconds, a metrics exporter collecting full stacks every scrape, an on-call engineer running a class histogram on a live node at peak. No single answer fits every service, so a lead has to set one, measure it and revisit it. ## Step 1: put a number on each tool The JDK 25 safepoint log names the requester of every stop, so the cost is measurable rather than argued: 1. Keep `-Xlog:safepoint` on, rotated to a file, on every service. 2. Sum `Total` by operation name per service per day, and split `Reaching safepoint` from `At safepoint`. 3. Attribute each name to a requester: `PrintThreads` with `FindDeadlocks` for thread-dump commands and SIGQUIT, `ThreadDump` for in-process `ThreadMXBean` stack collection, `GC_HeapInspection` for class histograms, `HeapDumper` for heap dumps, `RedefineClasses` for agents. That turns "is our tooling expensive?" into a ranked table. ## Step 2: rules by the shape of the cost | Shape | Typical names | Rule to consider | What you give up | |---|---|---|---| | Frequent and moderate | `PrintThreads`, `ThreadDump` | Cap the frequency, or use a capture that needs no global stop | A consistent all-thread instant | | Rare and huge | `HeapDumper`, `GC_HeapInspection` | Only on an instance out of rotation, or on the error path | Evidence from the exact live moment | | Bursty at start or attach | `RedefineClasses` | Load agents at start-up; restrict runtime retransformation | Attaching new instrumentation on demand | | Collector work | the collector's own names | Out of scope for tooling policy | none | ## Step 3: know the alternatives and their limits - **`jcmd <pid> Thread.dump_to_file`** on JDK 25 takes each thread's snapshot through a **per-thread handshake** rather than one global safepoint, so each thread stops only briefly while the others run; an unmounted virtual thread is read without one. The cost: the snapshots are taken at slightly different moments, so a lock-owner chain read from it can be inconsistent in a way a `Thread.print` dump is not. - **`ThreadMXBean.getThreadInfo` with a maximum depth of 0** collects thread states without stacks, and the JDK 25 source skips the VM operation entirely for that case. A metric that only needs counts per state should use it; asking for stacks, or calling `dumpAllThreads`, runs the `ThreadDump` safepoint. - **Sampling profilers** such as async-profiler collect Java stacks outside safepoints, so continuous stack sampling need not be done with scheduled thread dumps at all. - **Class histograms without `-all`** request a full collection first, which on Serial, Parallel and G1 runs inside the same stop; that is the expensive form, and the one most often run by habit. ## Step 4: decide the standing rules There is no universal right answer; these are the choices a lead owns: - **Frequency budget.** For example, at most one global-safepoint dump per instance per minute from automation, measured against the service's pause objective. - **Where heavy captures run.** Heap dumps and live histograms on a drained instance, or automatically on `OutOfMemoryError`, never by hand on a node serving traffic. - **Agents.** JDK 25 still allows loading an agent into a running JVM by default (`EnableDynamicAgentLoading` defaults to `true`) and only warns when it happens. Requiring agents on the command line moves their instrumentation to class-load time and start-up, before traffic arrives, instead of `RedefineClasses` stops on a node at peak, and it makes the set of agents reviewable. - **Exceptions with an owner.** An incident may justify a `Thread.print` on a live node, because a consistent instant is what you need; record it. ## How to know the policy works Re-run the per-operation sums after each rule change. The non-GC share of stopped time should fall while the evidence available during incidents stays sufficient. If on-call loses the dumps it needs, the budget was set too tight, and that is a judgment to revisit, not a rule to defend.

  • When would you insist on Thread.print despite its global stop?
    During an incident where the question is a lock-owner chain or a deadlock across threads. A global-safepoint dump captures every thread at one instant, so owner and waiter are consistent. Per-thread handshake snapshots are taken at slightly different moments and can show a chain that never existed at once. For that diagnosis, one extra stop is cheaper than a wrong conclusion.
  • A metrics library exports thread-state counts and the safepoint log shows a ThreadDump stop every scrape. What do you change?
    The library is asking for stacks it does not need. Thread-state counts only need `ThreadMXBean.getThreadInfo` with a maximum depth of 0, which the JDK 25 source serves without a VM operation, or no thread info at all. I would configure or replace the collector so it stops requesting stack depth, then confirm the `ThreadDump` lines disappear from the safepoint log.

  • Diagnostic tooling is free; only the collector costs pause time
  • Ban every stop-the-world diagnostic in production outright
  • Thread.dump_to_file captures one consistent instant like Thread.print
  • Any ThreadMXBean thread-info call stops the world
  • JDK 25 already refuses agents loaded into a running JVM by default

Assumes JDK 25: Thread.dump_to_file's per-thread handshake snapshots, the depth-0 ThreadMXBean path and the EnableDynamicAgentLoading default are those in jdk-25.0.4.