Javarevisited
Published in

Javarevisited

VisualVM: All-in-One Java Troubleshooting Tool

VisualVM, my best friend when it comes to troubleshooting my Java applications.

Photo by NeONBRAND on Unsplash

My project was facing a few problems related to memory issues recently, and I have the opportunity to work on them to fix them. In this article, I will be writing a simple guide on how to kickstart using VisualVM on your local machine and application.

To Install

VisualVM is available for all Microsoft Windows, Linux, and macOS. You may visit the VisualVM download page to download the software.

If you are using SDKMAN, a Software Development Kit Manager, you may use the following command to install:

## to find out the versions available
sdk list visualvm
Screenshot of the list command
## to install a specific version (e.g. 2.1.2)
sdk install visualvm 2.1.2

Intro

Once you installed and launched the application, you will see a screen like below.

Screenshot of the VisualVM startup

On the left panel, you will find all your local and remote Java applications.

Screenshot of a selected application

If you click on one of the applications, you will see the selected application data on the right. It consists of 5 tabs:

  • Overview: Displays the general information about the application and the runtime environment
  • Monitor: Displays monitoring data for CPU, Heap, Classes, and Threads. You can also find a button to Perform GC (Garbage Collection) and Heap Dump
  • Threads: Displays real-time and high-level data on thread activity
  • Sampler: To do CPU sampling and memory sampling
  • Profiler: To start and stop the profiling session of a local application

Example

Below are 2 screenshots of the before and after code optimization to improve my application CPU and heap space usage.

Before code optimization
After code optimization

From the heap space tab, we can see that the heap space used is lesser after the code optimization compared to the one before the code optimization. We also observed that the heap space did not keep increasing to about 2GB after code optimization. This is good news for our application as we have more available heap space to spare. Similarly, for CPU usage, we can observe that the CPU used is lesser and it does not cross the 40% mark compared to before code optimization.

So what did I do for the code optimization? I have:

  • found and removed redundant conversion of JsonArray to String, then later parse it to JsonArray from String
  • followed the excel library (Apache POI) recommendation to use the File way instead of InputStream as we could be dealing with large excel files
Screenshot of the Excel library documentation
  • modified code to close resource after used using Java 8 try-with-resources as I have noticed a message keep appearing in my logs
Cleaning up unclosed ZipFile for archive unknown archive

And there we have it. I hope you have found this useful. Thank you for reading. If you enjoyed this article, remember to follow me for more updates!

Stay tuned for more articles ✌️.

If you are not a Medium member yet and want to become one, click here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store