Instrumentation collects runtime information by applying probes to a program’s execution. It helps answer focused questions that sampling alone may leave open, such as which monitor blocks a thread or which database connections remain open. section 9.2 section 8.1
Start with sampling to see where execution spends time, then use instrumentation to investigate a specific concern in more detail. In JProfiler, select Instrumentation when attaching to a process; VisualVM calls the corresponding approach profiling. section 9.2 section 8.1
Investigating thread locks
Suppose a method’s total self-execution time is much larger than its CPU self time. It may be waiting on a lock rather than doing work. Profile locks to find the monitors and threads involved, the number of blocking events, and their duration. In one producer–consumer investigation, the threads blocked each other thousands of times using the same monitor. section 9.2
Figure 9.8 The profiling results give us a good understanding of what creates locks and what is affected by them. We see that there’s only one monitor the producer thread works with. Also, the producer thread was blocked by the consumer thread 3,698 times using the monitor. Using the same monitor instance, the producer blocked the consumer for a similar number of times: 3,699.
The lock results identify the shared monitor and show how often each thread blocked the other. section 9.2
Finding a JDBC connection leak
In JProfiler, use Start Center and Quick Attach to select the running Java process, choose Instrumentation, and start JDBC profiling under Databases. Reproduce the request, then inspect Connections and Connection Leaks. Many connections are not necessarily a problem, but connections still open long after the response indicate a leak. Enable CPU profiling to obtain a stack trace showing where leaking connections were created. section 8.1
Figure 8.10 The Connection Leaks tab shows us the status of each connection. We are interested in the connections that close late or never. Here, the connections the app opened are still alive long after the endpoint sent a response back to the client, which strongly indicates a problem.
The connection status view shows connections still alive long after the request completed. section 8.1













