Week 152 — How can one use tools like VisualVM to profile Java applications?

Question of the Week #152
How can one use tools like VisualVM to profile Java applications?
12 Replies
MrMisha | Coder
VisualVM is a open-source "All-in-One Java Troubleshooting" software which lets you analyze and profile the heap dumps, CPU usages, memory allocations in both local and remote setting. Installation of VisualVM VisualVM is shipped as a portal standalone tool, you may launch by executing the binary inside the bin/ folder (either ./bin/visualvm or ./bin/visualvm.exe based on your OS.)
Materials/Softwares/visualvm_22 $ tree
├── bin
│   ├── visualvm
│   └── visualvm.exe
├── etc
│   ├── visualvm.clusters
│   ├── visualvm.conf
│   ├── visualvm.icns
│   └── visualvm.import
├── LICENSE.txt
├── platform
│   ├── config
...
Materials/Softwares/visualvm_22 $ tree
├── bin
│   ├── visualvm
│   └── visualvm.exe
├── etc
│   ├── visualvm.clusters
│   ├── visualvm.conf
│   ├── visualvm.icns
│   └── visualvm.import
├── LICENSE.txt
├── platform
│   ├── config
...
No description
MrMisha | Coder
Connecting to the Application VisualVM supports profiling by either using a JMX Remote Connection or by directly hooking into an exisiting local JVM. For Local JVMs, You just find the PID of your application inside the Local tab group and double click to attach.
No description
MrMisha | Coder
For remote applications, the setup is a little complex as you'd have to setup JMX (Java Management Extensions) Remote Connection. To do this, you have to launch your program with additional java parameters.
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-jar yourapp.jar
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-jar yourapp.jar
You may read more about authentication and other parameter over @ https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html. After doing so, you may go over to the native menu, File > Add JMX Connection and fill in the connection details, including the hostname, port, and security credentails.
JDK 25 Documentation - Home
The documentation for JDK 25 includes developer guides, API documentation, and release notes.
No description
MrMisha | Coder
After that, your application should be visible on the Local tab group. If you have configured a remote host, it'll show up in the remote host section respectively. Once you double click on the application, it should open a new tab in the main content area with your server details, JVM arguments, System properties and other metadatas.
No description
MrMisha | Coder
Monitor Tab allows you to view statisitics about CPU, Heap allocation, Total classes, Threads and Virtual Threads. Furthermore, you're able to use the "Perform GC" option to perform garbage collection and measure CPU spikes/heap reduction during any sort of garbage collection.3 The Heap Dump feature allows you to create a snapshot of the memor/all objects and classes allocated in the application's memory for further profiling.
No description
MrMisha | Coder
Threads Tab allows you to monitor exisiting threads as well as check if they're running, parked, waiting for resource, etc.. You may also use the Thread Dump feature to get a more comprehensive details on what each thread is spending most of it's time on.
No description
No description
MrMisha | Coder
Sampler Tab lets you profile through sampling either CPU time or Memory allocations to find what methods/functions are consuming the most amount of resources, you can easily find hotspots with this tool.
No description
MrMisha | Coder
Heap Dump showcase You're able to filter classes. Sort by either instance count or size. Find what objects store the most amount of data and detect memory leaks.
No description
No description
MrMisha | Coder
Finally, the Profiler Tab (Instrumentation mode). Profiler tab lets you track each allocations/CPU, where it is originating from, as well as track locks, JDBC calls, memory access and much more. You simply put the classes you want to profile in the Profile classes: text section and click either one of the profiles in Profile section.
No description
MrMisha | Coder
After that, profiling will begin, once you're done, just click on the "Stop" button. Please bare in mind that Profiling through Instrumentation mode is blocking. It rewrites bytecode to record every method entry/exit, causing significant slowdown (often 5-50x). When you're profiling in production, it's usually best if you were to profile through sampling (Sampler Tab). It's non-blocking and periodically samples stack traces with minimal overhead (<5%).
MrMisha | Coder
This is just a quick 101 guide to Visual VM and what each feature inside does. For more in-depth exploration, I suggest you to consult the VisualVM's Documentation which is more comprehensive and goes into much more details: https://visualvm.github.io/documentation.html.
⭐ Submission from daysling

Did you find this page helpful?