Video in progress

A service with a 64 GB heap has a slow leak, but taking a heap dump causes a 40-second pause. How do you investigate?

Before a dump, I work with cheap signals: class histograms, JFR's OldObjectSample event and an allocation profile. If I need a dump, I take the instance out of traffic first, or reproduce the same leak on staging with a small heap.

📖 Answer

On a big heap a dump brings several problems: an STW pause, a 64 GB file on disk, and a machine to analyze it. So I go step by step.

  1. The histogram trend. jcmd <pid> GC.class_histogram is also STW, but much faster than a dump. I take it twice an hour apart and compare: which classes' instance counts keep growing.

  2. JFR Old Object Sample. JFR samples long-lived objects on the heap and remembers where they were created. If you take the dump with path-to-gc-roots=true, you can even see who is holding them:

jcmd <pid> JFR.start name=leak settings=profile
# a few hours later
jcmd <pid> JFR.dump name=leak path-to-gc-roots=true filename=leak.jfr

The "Memory Leak" page in JMC shows this data. It is the cheapest way to reach the source of a leak without a dump.

  1. Draining the instance. If I still need a dump, I take the pod out of the load balancer and then take the dump. The pause doesn't touch customers. In Kubernetes you can change the label to remove the pod from the Service selector.

  2. Reproducing at a small scale. If the leak depends on a type of traffic (for example, a particular endpoint), I restart it on staging with a 2 GB heap and analyze the dump quickly.

In parallel I look at the metrics: does the "heap after GC" line grow with the load or with time? If it grows with time, it is something like a cache or a registry. If with the load, something is being left behind per request.