This chapter introduces profiling as a practical way to uncover hidden issues in Java applications, especially when a codebase is unfamiliar, poorly designed, or too large to inspect manually. The central idea is to start with sampling, which gives a high-level view of what code is running, where time is being spent, and which threads or methods deserve closer attention. Sampling is presented as the best first step because it quickly reveals where to focus, and in many cases it is enough on its own to explain a slowdown or point directly to the problematic area of the app.
The chapter then shows how deeper profiling can reveal more than just “what runs”: it can identify how often methods execute, how much CPU time they consume, and whether the app is actually working or simply waiting on something external. Using examples with a delayed HTTP call, the profiler makes it clear that long execution time does not always mean high CPU usage; a method may appear slow because it is blocked waiting for a response. The same approach is used to inspect calls made through dependencies and frameworks, highlighting that profilers can expose work done behind abstractions such as dynamic proxies, which are often hard to understand by reading source code alone.
Finally, the chapter applies profiling to database access, showing how it can reveal SQL statements sent directly through JDBC as well as queries generated behind the scenes by frameworks like Hibernate and Spring Data JPA. This is especially useful for spotting inefficient patterns such as repeated queries and the N+1 problem, and even for diagnosing subtle mistakes in criteria queries that produce unnecessary joins. Across all examples, the chapter’s main message is that profiling saves time, provides a reliable path through complex execution flows, and helps you identify whether the real issue lies in your code, the framework, or an external dependency.
The app we are investigating exposes an endpoint: /demo. When you call this endpoint, you must wait for 5 seconds for the app to respond. We need to understand why it takes so long for the endpoint to respond. We know our app calls a mock endpoint from httpbin.org, which causes the delay, but we want to learn how to investigate this scenario with a profiler. This way, you’ll know how to use similar techniques for real-world situations.
When the endpoint is called (in this figure, using cURL), the app takes about 5 seconds to respond. In our scenario, we use a profiler to investigate this latency problem.
To start sampling the execution, select the process from the list on the left side, and then select the Sampler tab.
The profiler shows all the active threads in a list. You can expand each item to see the execution stack and an approximate execution time. When the app executes, the newly created threads appear in the list, and you can analyze their execution.
The stack trace shows what the app executes. You can see every method and each subsequent method that is called. This view helps you quickly find the code you want to focus on when investigating a certain capability.
When you expand the execution stack, you find which methods execute and how much time they spend executing. You can also deduce how long they wait and how much they work. The profiler shows both the app’s codebase methods and the methods called from specific dependencies (libraries or frameworks) the app uses.
The framework keeps the implementations for an abstraction separate and provides them dynamically during execution. Because the implementation is decoupled and the app provides it during run time, it’s more difficult to find it by reading the code.
Profiling a part of the app during execution to get details about the times a given method was invoked. We can see that the method causing the 5 seconds of latency is invoked only once, meaning the number of invocations doesn’t cause a problem here.
When profiling the app, we observe that one of the methods is called 10 times. We now need to ask ourselves if this is a design issue. Since we now have a big picture of the entire algorithm and we know what code is executed, we can also debug the app if we can’t figure out what happens.
The profiler intercepts the SQL queries the app sends to the DBMS through the JDBC driver. This gives you an easy way to get the queries, run them, observe what part of the codebase runs them, and know how many times a query is executed.
In a Java app, the communication with a relational DBMS is done through the JDBC driver. A profiler can intercept all method calls, including those of the JDBC driver, and retrieve the SQL queries the app sends to a DBMS. You can get the queries and use them in your investigations.
For each query, the profiler also provides the execution stack trace. You can use the stack trace to identify which part of your app’s codebase sent the query.
Even when working with a framework, the profiler can still intercept the SQL queries. This makes your investigation a lot easier because you can’t copy the query directly from the code as you can when using JDBC and native queries.
The profiler can intercept any SQL query sent to the DBMS through the JDBC driver. Here, we spot an issue in the generated query—an unneeded cross join that causes performance problems.
By eliminating the supplementary select() method call, the cross join disappeared. However, the overall algorithm for this app should be revised, since it still runs the same query multiple times, which is not optimal.
Summary
- A profiler intercepts the app’s execution and provides essential details about the code in execution, such as the execution stack trace for each thread, how long it takes for each method to execute, and how many times a certain method was called.
- When investigating latency problems, the first step to using a profiler is sampling—a way for the profiler to intercept the executing code without getting many details. Sampling is less resource consuming and allows you to observe the big picture of execution.
- Sampling gives you three essential details:
- What code executes—When investigating an issue, you sometimes don’t know what part of the code executes, and you can find this aspect by sampling.
- Total execution time of every method—This detail helps you to identify what part of the code causes potential latency problems.
- Total CPU execution time—This detail helps you to identify whether your code spends the execution time “working” or waiting for something.
- Sometimes sampling is enough to understand where a problem comes from. But in many cases, you need more details. You can get these details profiling the execution.
- Profiling is a resource-consuming process. With a real-world app, it’s almost always impossible to profile the whole codebase. For this reason, when profiling for details, you should filter specific packages and classes on which you want to focus your investigation. You can usually determine what part of the app to focus on by sampling the execution first.
- An essential detail you get by profiling is the number of invocations of a method. When sampling, you know the total time a method spends executing, but not how often it was called. This aspect is important in identifying a method that is slow or wrongly used.
- You can use a profiler to get SQL queries the app sends to a DBMS. The profiler intercepts any queries, regardless of the technology used to implement the app’s persistence layer. This is invaluable when investigating slow queries for apps that use frameworks (such as Hibernate) to work with a database.