Advanced visualization tools can make profiling data much easier to understand when a Java application misbehaves in ways that are hard to diagnose from code or logs alone. The chapter begins by showing how a seemingly simple request can reveal a deeper infrastructure issue, such as a JDBC connection leak that causes a service to stall after the first successful call. It emphasizes that even when frameworks manage resources for you most of the time, edge cases and lower-level integrations can still leave connections open and exhaust the database’s available pool.
To uncover such problems, the chapter uses a profiler with richer diagnostics than standard tools can provide. After reproducing the failure, the investigation moves from basic sampling to more detailed instrumentation, where the profiler’s database views expose live connections, suspiciously long-lived resources, and leak indicators. Once CPU profiling is enabled as well, the tool can show the stack trace responsible for each leaked connection, making it possible to pinpoint the exact method that forgot to close the resource and then confirm the fix by verifying that connections are properly released.
The chapter also explores two other visual approaches to profiled execution. Call graphs help readers understand an application’s structure by showing how methods and objects relate to one another across the execution chain, which is especially useful in unfamiliar or messy codebases. Flame graphs provide a different perspective on the same execution data by emphasizing where time is spent, helping reveal latency hotspots and compare method costs at a glance. Finally, the chapter extends profiling beyond relational databases by showing that similar techniques can also be used with NoSQL systems such as MongoDB, where profilers can intercept database operations, display their call trees, and help trace performance or behavior issues back to the application code that triggered them.
DBMS allows an app to open a finite and usually small number of connections. When the app reaches the limit of connections it can open, the DBMS doesn’t allow the app to open other connections. In such a case, the app may become unable to execute its work.
When calling the /products endpoint the first time, the app responds instantaneously with a list containing the word “Chocolate.” But when you try to call the endpoint a second time, the app appears to be stuck for about 30 seconds and then shows an error message.
After sampling the execution, we have more reasons to suspect something is wrong with establishing a connection to the DBMS. In the execution stack, we see that the app waited for 30 seconds to get a connection.
Start a sampling or profiling session with JProfiler by selecting the Start Center menu in the upper-left corner of the JProfiler window.
In the pop-up window, select Quick Attach and, in the list, select the process you want to profile. Then select the Start button to begin the profiling session.
To analyze the execution in more depth, we need to select Instrumentation, the equivalent to what we call profiling in VisualVM.
You can start JDBC profiling with JProfiler by first selecting JDBC in the left menu; then you begin the profiling process.
The Connections and Connection Leaks tabs show details about the connections the app creates, including potential problematic connections. We’ll use these details to understand where the problem in our app comes from.
The Connection Leaks tab confirms our suspicion (figure 8.10); not only does the app Figure 8.9 By sending a request to the /products endpoint, we see that the app creates many connections. We don’t know exactly what the app does, but this can be alarming.
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.
With CPU profiling enabled, JProfiler shows the stack trace, which helps you identify which part of the app’s code creates the leaking connections.
After fixing the error, we use JProfiler to confirm that no more connections are leaking. We observe that the app opens only one connection at a time and correctly closes the connection when it no longer needs it. The Connection Leaks tab doesn’t show any other faulty connections.
Select Call Tree from the left-hand menu to start profiling the CPU (recording CPU data). Send a request to the /products endpoint, and the profiler will initially show the recorded data as a stack trace, including details about the number of invocations and the execution time.
To get a call graph representation from an execution stack trace, right-click a line in the stack trace and select Show Call Graph.
A call graph shows the execution, focusing mainly on the relationship between objects and method calls. You can linearly navigate the method execution chain to determine where each method is called from and what that method calls. The call graph also shows objects and methods that are part of the app’s codebase and those of libraries and frameworks the app uses.
To change an execution stack (call tree) into a flame graph, select Analyze in the menu and then select Show Flame Graph.
The flame graph is a stack representation of the execution trace. Each level shows the methods called by the level below. The first (bottom) level of the stack is the start of the thread. This way, we see the execution stack vertically, while the flame graph horizontally indicates the time spent by each level relative to the level below.
When multiple methods share the same level, they are all called by the method below them. The sum of the lengths of the representation equals the length of the method under them. Each method’s length is a relative representation of the execution time from the total. In this case, findAll() took much longer to execute than findById().
To color your flame graph and make it easier to read, add colorization rules using the Colorize item in the top menu. The rules define which layers in the flame graph should be colored and which color should be used.
Colorizing levels helps highlight specific parts of the flame that you want to focus on. You can use multiple colors at the same time, which can help you to compare execution times.
JProfiler can intercept the operations an app applies to a NoSQL database. In this example, JProfiler intercepts two events: an update and a read on a document named “product.” This way, you can monitor the interaction between your app and a NoSQL database and consider the number of invocations for specific operations and the execution time. The profiler also gives you the complete stack trace for a particular operation so you can quickly find the code that caused a specific event.
Summary
- Free tools such as VisualVM offer plenty of widgets that can help with any investigation. But licensed tools such as JProfiler can make the investigations even more effective through different ways of representing the investigation data.
- Sometimes apps encounter issues when connecting to a DBMS. By using JProfiler, you can more easily investigate issues with a JDBC connection to a relational database server. You can evaluate whether connections remain open and identify the part of the code that “forgets” to close them.
- Call graphs are a different way to visualize the execution stack and focus mainly on the relationship between objects and method calls. For this reason, call graphs are an excellent tool you can use to more easily understand the class design behind the app’s execution.
- Flame graphs offer a different perspective for visualizing profiled data. You can use flame graphs to more easily spot areas causing latencies in execution and long stack traces. You can color specific layers in a flame graph to better visualize the execution.
- Some licensed tools offer extended capabilities, such as investigating the communication between apps or between your app and a NoSQL database server.