Profiling gathers information about a running application to reveal what executes and where time or memory is spent. It is especially useful when unfamiliar code makes it hard to find the cause of a behavior. A profiler can also help locate code worth examining with a debugger. section 7.0 section 2.3 section 11.1
Start with sampling for a broad, less resource-intensive view. If that is not enough, use detailed profiling, also called instrumentation, on the relevant packages or classes. Sampling can show execution time and CPU time, but it does not give method invocation counts; instrumentation can provide those counts. section 7.1 section 7.4
A practical investigation follows a narrowing process:
- Reproduce the slow or memory-intensive capability while sampling the application. Use the results to find code or object types that deserve closer inspection. section 7.1 section 11.1
- Filter detailed profiling to the relevant packages or classes. Instrumenting an entire real-world codebase can consume too much time and resources. section 7.2
- Interpret the details in context: a method may be slow per call or may be called unnecessarily often. Follow execution stacks back to application code when you need to inspect the implementation. section 7.4 section 7.3.1
Investigating a delayed endpoint
An endpoint takes about five seconds because it calls a delayed external endpoint. Sampling locates the executing code; detailed profiling then shows that the method responsible for the delay was invoked only once. Repeated calls are therefore not the cause of this delay. section 7.1 section 7.2
Figure 7.8 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.
See the profiling result and package restrictions for the delayed method. section 7.2
For database investigations, VisualVM can profile JDBC calls and retrieve SQL queries before they reach the DBMS. It also shows query counts and execution stacks. In one purchase lookup, a product query runs ten times; inspecting the calling code suggests replacing the repeated lookups with a join. section 7.3.1
Figure 7.12 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.
Use the query’s execution stack to find the application code that sent it. section 7.3.1
For memory issues, sample object allocations first and sort by memory occupied and instance count. A problem may involve many instances or a few large ones. Then profile selected types for more detail and use their allocation stack traces to find where the objects were created. section 11.1
Profiling is not the only investigation tool. If an application has already stopped, it cannot be profiled in execution; logs or a heap dump may help instead. For an application blocked entirely, a thread dump can be a more direct starting point. section 11.0 section 2.3











