Sub topic 17
Profiling safely in production
This chapter assumes you can record a Java Flight Recorder (JFR) profile, read GC logs, and use jcmd. Production adds one rule: the measurement must not become the incident.
Why production is different
Some problems only appear under real traffic, real data sizes and real contention. A staging profile can miss them. The cost is that every probe uses CPU, memory or disk on a system that users depend on.
Choose low-overhead tools first
JFR is built into the JDK and is designed to run continuously. The default settings usually add around one percent overhead. The profile settings sample more often and cost more, so use them for short windows.
| Technique | Typical cost | Use it when |
|---|---|---|
| JFR `default` settings | Low | Always-on recording |
| JFR `profile` settings | Moderate | A short, targeted capture |
| Thread dump | Brief pause | Suspected deadlock or blocking |
| Heap dump | Long pause, large file | Confirmed leak, with the instance drained from traffic if possible |
| Instrumenting profiler | High, distorts timings | Rarely, and not on a live node |
Record without restarting
Attach to the running process with jcmd. Give the recording a name and a limit so it cannot run forever.
jcmd <pid> JFR.start name=incident settings=default maxage=30m maxsize=250m
jcmd <pid> JFR.dump name=incident filename=/tmp/incident.jfr
jcmd <pid> JFR.stop name=incidentmaxage and maxsize keep a rolling buffer, so the recording is already there when you notice a problem. Dump it after the event instead of trying to reproduce it.
Protect the system
- Limit the recording size so it cannot fill the disk.
- Write files to a volume that the application does not use for its own data.
- Treat recordings and heap dumps as sensitive. They can contain request data, tokens and passwords.
- Start with one instance, not the whole fleet.
- Take a heap dump only when you need one. It pauses the JVM, and a multi-gigabyte file takes time to write and copy.
A safe procedure
- Write down the symptom and the metric that shows it, such as p99 latency.
- Pick one instance that shows the symptom.
- Capture with the lowest-cost tool that can answer the question.
- Analyze the file offline in JDK Mission Control, not on the server.
- Form a hypothesis, then test the fix in a non-production environment.
- Roll out the fix to one instance, compare against the others, then widen.
Common errors
- Starting with a heap dump when a thread dump or JFR would answer the question.
- Leaving a high-frequency recording running after the incident.
- Comparing before and after numbers from different load levels.
- Profiling one instance and assuming every instance behaves the same.
Exercise
Latency on one production node rises every afternoon. You suspect GC pressure, but you cannot restart the service. Which tool and settings do you start with, and how do you stop the recording from filling the disk?
Show answer
Attach with jcmd and start a JFR recording with the default settings, for example JFR.start name=afternoon settings=default maxage=30m maxsize=250m. Dump it when latency rises, and analyze the file offline in JDK Mission Control. The maxage and maxsize options keep a rolling buffer, so disk use stays bounded. Use the profile settings only for a short window if the default data is not enough.