Family man and tech geek - passionate about Software & Data Engineering, and Entrepreneurship
According to Wikipedia, Software profiling is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls.
Most commonly, profiling information serves to aid program optimization. Profiling is achieved by instrumenting either the program source code or its binary executable form using a tool called a profiler (or code profiler).
One of the most popular profilers in the Java world is VisualVM. It is a visual tool that integrates command line JDK tools and lightweight profiling capabilities. The project is Open Source and maintained by Oracle, you can find the codebase here. The tool was designed for development and production time use.
VisualVM was included in the JDK until version 8. Some of its main features:
You could find more information about the different features in the project’s webpage, but definitely I would suggest you to just download it and start playing around, it’s “easy” to use.
Recently I needed to profile a Spring Boot app and made some interesting findings that I wanted to share since these are not really obvious.
Something really cool about Spring Boot is FAT JARs, by “default” you can benefit from one single executable JAR with all the app classes and dependencies in it. One JAR, one App, just execute it and that’s it!
FAT JARs are definitely powerful, but there are some tradeoffs when you use them. For example when creating Docker images.
And… When you need to profile an application using VisualVM! Explode your FAT JAR. This is fundamental, I wasted a lot of time before on a Windows machine with JDK 8 trying to profile an unexploded JAR (it failed silently).
On my current computer, a Mac with JDK 12, the profiler fails to start! (ERROR: Profiler engine warning: class *FAKE_CLASS_1* that should be instrumented is not loaded by target VM). It’s clear, the classes cannot be found therefore cannot be instrumented.
I’ll demo my point using… The Spring Petclinic Demo App! The best Spring demo project ever!
Clone the repo, build the app and explode the JAR file in a new folder:
git clone https://github.com/spring-projects/spring-petclinic.git cd spring-petclinic mvn package mkdir jar-content cd jar-content/ jar xvf ../target/spring-petclinic-2.4.0.BUILD-SNAPSHOT.jar
At the end of the process, you should end up with the following folders:
BOOT-INF: Contains all classes and dependencies META-INF: Contains all JAR metadataorg: Contains Spring Boot launcher classes
It’s of special interest the file META-INF/MANIFEST.MF, there you’d find what are your main and start classes. With all that info you’re ready to execute/start your app:
java -cp .:BOOT-INF/lib/*:BOOT-INF/classes org.springframework.boot.loader.JarLauncher org.springframework.samples.petclinic.PetClinicApplication
NOTE: If you’re using Windows, remember to change “:” by “;” and slashes by backslashes.
There you go, that’s all we need for now!
In order to profile your app, download VisualVM and execute it. You should see your app configured under the Local menu. In case you’re trying to profile a remote app keep in mind the following configurations
Double click on your app, go to the profile tab and change the profile class to match your Spring Boot app root package! Important: add “.**” to the end (to include all classes in that package). For example: org.springframework.samples.petclinic.**
One last final tip: from my experience the best view is the Hot Spots, it allows you to quickly see where are your potential bottlenecks.
Software profiling is definitively very useful when trying to understand bottlenecks and potential areas of improvement. A must to know tool for understanding your application and tuning it.
Tools like VisualVM really empower you and are super useful to have in your toolbox. Especially useful, the Open Source nature of the tool, you’d find tons of related information out there!
And finally, but not least, yes, you can profile a Spring Boot App! In later articles I might write about heap and stack dumps, and see how you can even use Spring Boot Actuators for getting that information.
Create your free account to unlock your custom reading experience.