Sub topic 7
Exploring a Recording in JDK Mission Control
This chapter assumes you can start a Java Flight Recorder (JFR) recording, as covered in the previous chapter, and that you know what a heap, a garbage collector (GC) and a thread are. It shows how to open the resulting .jfr file in JDK Mission Control (JMC), what each page tells you, and which mistakes lead to wrong conclusions.
What JMC is and how to get it
JDK Mission Control is a desktop application that reads JFR files and shows them as pages, tables and graphs. It is a separate download and is not bundled with the JDK since Java 11. It runs on its own and does not need to be the same version as the JVM that produced the recording. Open a file with File > Open File, or start JMC with the file path as an argument.
If you have no desktop on the server, copy the file to your workstation. A recording is a self-contained binary file, so you can analyze it anywhere.
For a quick look without a GUI, the JDK includes the jfr command:
jfr summary app.jfr
jfr print --events jdk.GarbageCollection app.jfrjfr summary lists every event type in the file with a count. Use it to check that the recording contains what you expect before you transfer a large file.
Start with the Automated Analysis Results
When JMC opens a recording, it offers the Automated Analysis Results page. It runs a set of rules over the events and gives each one a score from 0 to 100. The rules are grouped into categories such as Java Application, JVM Internals, Garbage Collection and Environment.
| Score | Colour | Meaning |
|---|---|---|
| 0 to 24 | Green | No problem found |
| 25 to 74 | Yellow | Worth a look |
| 75 to 100 | Red | Likely problem |
Treat the page as a list of leads, not a diagnosis. A rule such as "Hot Methods" or "GC Pressure" tells you where to look next. It does not tell you the cause, and some rules depend on the recording settings. For example, a rule that needs allocation events reports nothing useful if the recording used the default settings and those events were thin or missing.
Each result has an explanation and often a suggested action. Read it, then open the page that supplies the evidence.
The main pages
The left side of the window has an outline of pages. The ones you will use most are these.
| Page | What it shows | Typical question |
|---|---|---|
| Java Application | Threads, method profiling, lock instances, file and socket I/O, exceptions | Where does my code spend time? |
| JVM Internals | GC, compiler, class loading, safepoints | Is the JVM itself getting in the way? |
| Environment | CPU use, memory, OS, system properties | Was the machine busy with something else? |
| Event Browser | Every event in the file, filterable | What exactly did the JVM record? |
Method Profiling
Under Java Application, the Method Profiling page lists the sampled stack traces. JFR takes a sample of the executing Java threads at a fixed interval. By default that is every 20 ms for the default settings and every 10 ms for profile. Each sample records the full stack trace of one thread.
The Stack Trace view groups samples. The top frame is where the thread was when sampled. Sorting by count shows which methods appear most often, which is the basis of CPU profiling in the next chapters.
Two points matter here:
- A count is a number of samples, not a time. With a 10 ms interval, 500 samples is about 5 seconds of sampled execution across all threads.
- The page shows the top frame and the whole stack. A method that is slow because of the code it calls shows low "self" counts but high counts in the stack below it. Check both before blaming a method.
Memory and garbage collection
The Memory page shows heap use over time, and the Garbage Collections page shows each collection with its pause time and cause. Look for these patterns:
- Heap use that returns to the same low point after every collection is normal.
- A low point that climbs steadily suggests objects are being kept alive. That is a hint of a leak, covered in a later chapter.
- Many short collections with a high allocation rate point to GC pressure.
- A few very long pauses matter for latency goals even if the average is small.
The Allocation tab uses jdk.ObjectAllocationInNewTLAB and jdk.ObjectAllocationOutsideTLAB events. These are sampled, not complete, so they show the main allocating stack traces and not an exact count.
Threads and locks
The Threads page shows a timeline per thread, coloured by state. The Lock Instances page lists monitors where threads blocked, and for how long. JFR records a contention event only when a thread waits longer than a threshold, which is 20 ms in the default settings. Short waits that happen millions of times will not show up as individual events.
Using the Event Browser and filters
The Event Browser lists every event type and lets you open the raw events. Use it when a page does not show what you need. Every event has a start time, a duration, a thread and often a stack trace.
The Outline view lets you select event types and see them on a timeline. To focus on a problem, use the range selector at the top of the page. Drag across the part of the timeline where latency spiked and all pages recompute for that window only.
This is the most useful habit in JMC. A recording of an hour includes startup, idle time and the one bad minute. Averaging across all of it hides the problem.
Reading a recording step by step
Suppose you recorded the Slow program from earlier, which concatenates strings in a loop, using the profile settings:
javac Slow.java
java -XX:StartFlightRecording=duration=60s,filename=slow.jfr,settings=profile SlowOpen slow.jfr and work through it:
- On Automated Analysis Results, look for a high GC or allocation score. The loop creates a new
Stringon every iteration, so expect allocation-related findings. - On Method Profiling, find the frames at the top of the stack. The time goes into string building code called from
Slow.main. The sample count undermainshould be very high compared with other frames. - On the Memory page, look at the heap graph. You should see a rapid sawtooth: heap rises quickly and drops after each young collection.
- On the Garbage Collections page, check the pause lengths. They are usually small here, so the cost is the copying and allocation work, not the pauses.
The conclusion is that the program is dominated by allocation and copying, which StringBuilder would remove. You then change the code, record again and compare, which is the subject of the optimize-and-verify chapter.
Common errors
- Reading a short, idle recording. If the application had no load, the profile shows idle threads and the JVM's own background work. Record while the slow behaviour is happening.
- Looking at the whole file. Select the range of the problem first.
- Trusting a rule score alone. A red result with no supporting events on the detail page is a lead to check, not a finding.
- Treating sample counts as exact. Sampling gives a statistical picture. Small differences between two methods are noise.
- Missing events because of settings. If a page is empty, check the Event Browser. The event may not have been enabled, or its threshold may be too high.
- Comparing recordings made under different load. A change in numbers may reflect the load, not your code.
Exercise
You open a recording and the Method Profiling page shows 300 samples in a method parse() and 280 in format(). The recording used a 10 ms sampling interval. Can you say parse() is slower than format()? Name two other things you should check.
Show answer
No. The difference is small, and sampling is statistical, so 300 against 280 is within noise. The counts also represent about 3.0 s and 2.8 s of sampled time, not exact durations. Check the self-time against the stack-inclusive time for each method, since either could be slow because of what it calls. Also select the time range of the actual problem so that startup or idle time does not distort the counts. Checking that the recording was made under realistic load is also a good answer.
Exercise
The Lock Instances page is empty, but you suspect threads are contending for a lock. Give one reason this can happen and one way to investigate.
Show answer
JFR records a monitor contention event only when a thread waits longer than a threshold, 20 ms in the default settings, so many short waits are not recorded as events. You can record again with a lower threshold for jdk.JavaMonitorEnter, or take thread dumps with jcmd <pid> Thread.print and look for threads in the BLOCKED state.