---
title: "Tail latency spikes. A garbage collection pause or a slow dependency? | Proofline"
description: "A stop-the-world garbage collection pause stops application threads across an instance at once, and the collector's own log records it."
url: https://proofline.sh/learn/latency-spikes-gc-pause-or-dependency/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# Tail latency spikes. A garbage collection pause or a slow dependency?

A stop-the-world garbage collection pause stops application threads across an instance at once, and the collector's own log records it. A slow dependency delays only the requests that called it, and the dependency's own timing shows the slow calls. Line up the spikes with the runtime's GC log and check whether the delayed requests on one instance finished together and included routes that never call the dependency.

Broken dependencies The Proofline team November 24, 2026 8 min read

## The symptom and two explanations

A fictional `cart-api` service runs on eight JVM instances. Its p99 latency is about 60 ms, with spikes to 400 or 500 ms a few times an hour, one instance at a time. Most slow requests are `/quote`, which calls the `pricing` service, and the client span around that call is long in the slow traces. Two explanations fit:

-   A garbage collection pause. The JVM stops application threads to collect, and every request on that instance waits until it resumes.
-   A slow dependency. Some calls to `pricing` take hundreds of milliseconds, and the requests that made them are slow.

The trace looks like it settles the matter, because the long span is the dependency call. It does not: a client span measures from the moment the call started to the moment the calling thread ran again, and a thread stopped by a pause cannot run again until the pause ends. The lab below shows a dependency span inflated by a pause in which the dependency answered in 5 ms. [Dependency slow for the client but fast on the server](/learn/dependency-latency-client-or-server/)

## What each explanation predicts

| Observation | GC pause | Slow dependency |
| --- | --- | --- |
| p99 spikes on one instance | yes | yes |
| Slow requests include routes that never call the dependency | yes | no |
| Slow requests on the instance finish within a few ms of each other | often, when several requests are ready to resume | no, one by one |
| Stopped time overlaps the slow requests and can account for their excess latency | yes | no |
| Dependency's own (server-side) time for those calls | normal | slow |
| Client span around the dependency call | can be long | long |
| Other instances slow in the same seconds | no, each pauses on its own | often, if they share the dependency |

The stopped-time row shows whether a pause could explain the delays; clustered completions give you a time window to check. The client-span row is where the investigation usually starts, and it predicts the same thing for both.

## The lab: a real pause and a slow call

This lab uses a real collection, not a simulation. Eight worker threads serve requests in a loop: every request spends 20 ms in local work, and half of them, `/quote`, then call a dependency that normally answers in 5 ms. In the `gc` world, another thread builds 1.5 million objects in reference cycles once a second and runs a full collection; in CPython only one thread runs Python bytecode at a time, so no request thread can resume while the collection runs. [Global interpreter lock](https://docs.python.org/3/glossary.html#term-global-interpreter-lock) In the `dependency` world there is no collection, and 3% of dependency calls take 250 ms. The script records each full collection's start and stop with `gc.callbacks`, which the collector invokes "before and after collection". [gc](https://docs.python.org/3/library/gc.html#gc.callbacks)

```
import gc, random, sys, threading, time

if len(sys.argv) < 2 or sys.argv[1] not in ("gc", "dependency"):
    sys.exit("usage: python3 pause.py gc|dependency")
world = sys.argv[1]
pauses, rows = [], []          # full-collection windows; (route, start, end, dependency's own s, client-timed s)
lock = threading.Lock()

def on_gc(phase, info):        # the interpreter calls this around every collection
    if info["generation"] == 2:
        if phase == "start":
            on_gc.t = time.perf_counter()
        else:
            pauses.append((on_gc.t, time.perf_counter()))
gc.callbacks.append(on_gc)

def worker(seed, stop):
    rng = random.Random(seed)
    while time.perf_counter() < stop:
        route = rng.choice(["/fast", "/quote"])
        start, own, timed = time.perf_counter(), 0.0, 0.0
        time.sleep(0.02)                         # local work every request does (disk, rendering)
        if route == "/quote":                    # the dependency call
            own = 0.25 if world == "dependency" and rng.random() < 0.03 else 0.005
            d0 = time.perf_counter()
            time.sleep(own)                      # what the dependency's server would log
            timed = time.perf_counter() - d0     # what a client span around the call records
        end = time.perf_counter()
        with lock:
            rows.append((route, start, end, own, timed))

def collector(stop):           # in the gc world, builds cyclic garbage and collects it each second
    while time.perf_counter() < stop:
        if world == "gc":
            junk = [[] for _ in range(1_500_000)]
            for a, b in zip(junk, junk[1:]):
                a.append(b); b.append(a)
            del junk, a, b
            gc.collect()
        time.sleep(1)

gc.disable()                   # only the collector thread collects, so the runs repeat
stop = time.perf_counter() + 6
threads = [threading.Thread(target=worker, args=(i, stop)) for i in range(8)]
threads.append(threading.Thread(target=collector, args=(stop,)))
for t in threads: t.start()
for t in threads: t.join()

slow = [r for r in rows if r[2] - r[1] > 0.1]
paused = [r for r in slow if any(r[1] < p1 and r[2] > p0 for p0, p1 in pauses)]
longest = max((p1 - p0 for p0, p1 in pauses), default=0)
bursts = []                    # slow requests that finished within 20 ms of each other
for end in sorted(r[2] for r in slow):
    if bursts and end - bursts[-1][-1] < 0.02:
        bursts[-1].append(end)
    else:
        bursts.append([end])
print(f"world={world}: {len(rows)} requests, {len(slow)} over 100 ms "
      f"(/fast {sum(r[0] == '/fast' for r in slow)}, /quote {sum(r[0] == '/quote' for r in slow)}), "
      f"{len(paused)} of them overlap a full collection")
print(f"  they finished in {len(bursts)} bursts, the largest {max(map(len, bursts), default=0)} requests")
print(f"  dependency calls over 100 ms: {sum(r[3] > 0.1 for r in rows)} by the dependency's own time, "
      f"{sum(r[4] > 0.1 for r in rows)} by the client's timer")
print(f"  full collections: {len(pauses)}, longest {longest * 1000:.0f} ms")
```

Save it as `pause.py`. Write down, for each world, whether any `/fast` request will be slow and how large the largest burst will be. Each run takes about six seconds:

```
python3 pause.py gc; python3 pause.py dependency
```

Python 3.12.3 on a Linux VM printed:

```
world=gc: 1480 requests, 32 over 100 ms (/fast 12, /quote 20), 32 of them overlap a full collection
  they finished in 4 bursts, the largest 8 requests
  dependency calls over 100 ms: 0 by the dependency's own time, 12 by the client's timer
  full collections: 4, longest 350 ms
world=dependency: 1898 requests, 20 over 100 ms (/fast 0, /quote 20), 0 of them overlap a full collection
  they finished in 18 bursts, the largest 2 requests
  dependency calls over 100 ms: 20 by the dependency's own time, 20 by the client's timer
  full collections: 0, longest 0 ms
```

In the `gc` world, four collections, the longest 350 ms, produced 32 slow requests: four bursts of eight, one for each worker that had a request in flight, `/fast` among them. The dependency answered every call in 5 ms, yet the client's timer recorded 12 calls over 100 ms, because those calls ended during a pause and their threads resumed only after it. In the `dependency` world only `/quote` was slow, the slow requests finished in small, unrelated groups, and the dependency's own time agreed with the client's.

The timings are real, so request counts and pause lengths differ between runs and machines; use the collection intervals and route counts to interpret your own run. The prediction fails if the `gc` world shows no slow `/fast` request and no burst as large as the worker count, or the `dependency` world shows either. To make the pause shorter or longer, change the `1_500_000`; to make the dependency's slow calls rarer, change the `0.03`.

## Read it on a real system

Turn on the runtime's collector log and read its pause times against the spike timestamps. On the JVM, unified logging writes GC events with `-Xlog:gc`, and the time application threads were stopped is logged under the `safepoint` tag, the replacement for `PrintGCApplicationStoppedTime`. The `utctime` decorator makes the lines comparable with request logs. [The java command, -Xlog](https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html)

```
java -Xlog:gc,safepoint:file=/var/log/cart-api/gc.log:utctime,uptime -jar cart-api.jar
```

This changes how the process starts, so roll it out as a configuration change. In Go, `GODEBUG=gctrace=1` makes the collector "emit a single line to standard error at each collection, summarizing the amount of memory collected and the length of the pause". [runtime](https://pkg.go.dev/runtime) In Python, register `gc.callbacks` as the lab does; from Python 3.15 the stop callback also receives the collection's `duration`. [gc](https://docs.python.org/3/library/gc.html#gc.callbacks)

Then read the two request-side observations for one instance and one spike:

-   the slow requests' routes, and whether any of them never call the dependency;
-   their end times, and whether they cluster within a few milliseconds.

For the dependency, compare the client span with the server span for the same call. A long client span over a short server span is time spent outside the dependency, a pause among the candidates. [Which trace span is on the critical path?](/learn/which-trace-span-is-on-the-critical-path/)

## Limits

A single-threaded event loop shows the same burst for any long synchronous task, not only for collection. A large JSON parse or a CPU-heavy request on the loop delays every request on it. The GC log row separates these: a burst with no collection in it is a blocked loop or a busy thread.

Mostly concurrent collectors such as G1 do some of their expensive work alongside the application, and Oracle's tuning guide gives ZGC maximum pauses under a millisecond, so a GC log full of collections does not mean long pauses. Read the stopped time, not the collection count. [Available collectors](https://docs.oracle.com/en/java/javase/21/gctuning/available-collectors.html) A pause can also come from outside the runtime, such as CPU throttling under a container limit or the host stealing CPU. Those also delay every request in flight at once, and the GC log is empty for them.

Both explanations can be true together, and they can feed each other: a slow dependency keeps more requests in flight, which keeps more objects alive and makes collections longer. When the spikes are regular, find the period first. [Latency spikes every few minutes](/learn/latency-spikes-every-few-minutes/) For choosing among more than two explanations, see [Choose the observation where explanations disagree](/learn/which-observation-separates-two-explanations/).

## Questions

### How do I confirm that GC pauses cause latency spikes?

Match the request and pause intervals on the same instance. A request may start before the pause and finish after threads resume. Compare the overlap with its excess latency over similar requests: a 3 ms stop cannot explain a 300 ms spike by itself. Check whether routes that skip the dependency also slowed and whether completions clustered after resumption.

### Why does the trace show the dependency call as the slow span?

The calling thread recorded the span's end only when it ran again. If a pause stopped it while the response sat waiting, the span includes the pause. The dependency's server-side span for the same call shows the time it actually took.

### Can a GC pause cause timeouts in other services?

Yes. Requests from other services to the paused instance wait for the pause too, and if the pause is longer than their timeout they fail. Their errors line up with the pause on that one instance, which is a way to find it from the caller's side.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Dependency slow for the client but fast on the server](/learn/dependency-latency-client-or-server/)
    
    Network outages The Proofline team November 23, 2026 7 min read
    
-   ### [Latency spikes every few minutes. Which timer is firing?](/learn/latency-spikes-every-few-minutes/)
    
    Capacity overruns The Proofline team November 24, 2026 8 min read
