This chapter introduces profiling as a practical way to illuminate JVM applications when symptoms such as slowness, strange thread behavior, or crashes are hard to explain. It emphasizes that a profiler is useful because it can show how an app uses CPU and memory, reveal which threads are running and in what state, and help you infer what code is active without immediately diving into source code. The chapter also frames profiling as an early investigation step that complements later tools such as thread dumps and heap dumps.
Using VisualVM as the main example, the text explains how to install and configure a profiler, connect it to a running Java process, and interpret its main views. The chapter walks through observing CPU and memory consumption, showing how unusual resource patterns can indicate concurrency bugs, zombie threads, or memory leaks. It contrasts unhealthy behavior, such as high CPU with little useful work or steadily rising memory usage, with healthier patterns where the JVM shows more balanced activity and garbage collection creates visible peaks and valleys.
The chapter illustrates these ideas with simple producer-consumer examples and a deliberately leaky application. In the concurrency case, unsafe access to a shared collection leads to race conditions, exceptions, and threads that either die or keep spinning uselessly; synchronization and better thread naming improve the behavior and make diagnosis easier. In the memory-leak case, objects are retained until the heap fills and an OutOfMemoryError occurs, reinforcing that fixing memory pressure requires understanding why references remain alive, not just increasing heap size. It also briefly notes that metaspace problems can occur when frameworks or reflection-heavy code are misused, and that temporarily increasing memory limits is only a stopgap while the real design issue is corrected.
VisualVM welcome screen. Once you configure and start VisualVM, you find that the tool has a simple and easy-to-learn GUI. On the left of the welcome screen are the processes running locally that you can investigate with the tool.
Double-click a process name to start using VisualVM to investigate that process, and a new tab will appear. In this tab are all the needed capabilities VisualVM provides for exploring that particular process.
If the tool doesn’t seem to be working properly, you need to check the way it is configured. Such problems can occur when the configured JVM distribution is not among those VisualVM supports. Sometimes the tool can’t connect to the local process you want to investigate for some reason. In such cases, use a different JVM distribution that complies with the tool’s requirements or review how the process you want to investigate was started.
Using VisualVM to observe the use of CPU resources. The widget in the Monitor tab shows you how much CPU the process uses and how much of the usage is caused by the GC. This information helps you to understand whether the app has execution problems and is excellent guidance for the next steps in your investigation. In this particular example, the process spends about 50% CPU. The GC doesn’t influence this value. These signs are often indicators of zombie threads that are usually generated by concurrency problems.
On the right side of the CPU usage widget, you find the memory usage widget. In this example, the app uses almost no memory. This is also why the GC activity is zero. An app not consuming any memory means the app isn’t doing anything.
The Threads tab offers a visual representation of the threads that are alive and their status. The widget shows all the process threads, including those started by the JVM, which helps you to easily identify which threads you should pay attention to and eventually investigate deeper using a thread dump.
If exceptions occur during an app’s execution, some of the threads may be stopped. This figure shows a case in which the concurrent access caused exceptions in three of the threads and stopped them. Only one thread remained alive. Remember, concurrency problems in multithreaded apps can cause different unexpected results.
After correctly synchronizing the code, the resource consumption widgets look different. The CPU consumption is lower, and the app uses some memory.
The Threads tab helps you to visualize the execution of the threads in your app. Since the threads’ names start with an underline, you can simply sort them by name to see them grouped. Notice that their execution is interrupted from time to time by the monitor, which allows just one thread at a time through the synchronized blocks of code.
An OutOfMemoryError is like a ticking bomb. An app fails to remove references to objects it no longer uses. The GC can’t remove these instances from the memory because the app keeps their references. While more objects are created, the memory gets full. At some point, there’s no more space in the heap to allocate other objects, and the app fails with an OutOfMemoryError.
When a memory leak affects your app, the used memory grows continuously. GC attempts to free the memory but cannot remove enough data. The used memory increases until the app can’t allocate any more new data. At this point, the app throws an OutOfMemoryError and stops. In many cases, a memory leak also causes intensified GC activity, which can be seen in the CPU resource usage widget.
A comparison between the memory usage for a healthy app versus an app suffering from a memory leak. The GC manages to free unneeded data from memory for a healthy app, and the allocated space never fills up. An app suffering from a memory leak doesn’t allow the GC to remove enough data. At some point, the memory fills up completely, generating an OutOfMemoryError.
The metaspace is a part of the memory used to store class metadata. In particular cases, the metaspace can be overflowed. A VisualVM memory allocation widget also shows the usage of the metaspace.
Summary
- A profiler is a tool that allows you to observe an app’s execution to identify the causes of certain problems that are more difficult to spot otherwise. A profiler shows you
- The profiler provides excellent visual widgets that help you to more quickly understand certain aspects.
- You can observe the GC’s execution using the profiler, which helps you to identify issues such as the app not correctly deallocating unused data from memory (memory leaks).