Identify and resolve performance bottlenecks, memory leaks, and threading issues
- Profilers in the Market
- JProfiler Install & License
- After download
- See Sample App Leak Memory
- Help and Docs
- Startup Settings
- Start and Stop GUI
- Session with Attach
- Remote access
- Watch it run
- Diffs in memory usage
- Biggest Objects
- Show source code
- Save Snapshot
- Root Causes
- JProfiler Origin Tracker
- ReTransform to Instrument Code
- Other Sample Programs
- JProfiler Social Media
- JMC (Java Mission Control)
- What to Look For
This tutorial aims to enable you to identify and resolve problematic patterns in memory and CPU usage in Java and Scala code using JProfiler and JVisualVM.
I want you to feel confident that you’ve mastered this skill. That’s why this takes a hands-on approach where you type in commands and we explain the responses and possible troubleshooting. This is a “deep dive” because all details are presented.
Like a good music DJ, I’ve carefully arranged the presentation of concepts into a sequence for easy learning, so you don’t have to spend as much time as me making sense of the flood of material around this subject.
Sentences that begin with PROTIP are a high point of this website to point out wisdom and advice from experience. NOTE point out observations that many miss. Search for them if you only want “TL;DR” (Too Long Didn’t Read) highlights.
Stuck? Contact me and I or one of my friends will help you.
Profilers in the Market
Comparison for Mac (named license with two years of support):
|JVisualVM||2016||GitHub 1.3.9||13 MB||free|
VisualVM has the largest market share. perhaps it is free and has been bundled in the JDK since version 6, update 7.
The bleeding-edge version at Visual VM GitHub is actively maintained by two developers in Prague, the Czech Republic.
VIDEO: Introduction to Java Visual VM from 2013.
JProfiler Install & License
for your platform.
PROTIP: While you wait for the download, identify the version of IDE you’ll be using with JProfiler. Most developers use integrations with their IDE IntelliJ IDEA, Eclipse, NetBeans, Oracle JDeveloper.
Also, get a license at
$698 with a year of support.
Open the JProfiler installer.
- On a Mac, drag the JProfiler icon to drop on the Applications folder.
- Exit the installer dialog.
- Delete (Move to Trash) the installer file to save disk space.
- Use Finder to initiate JProfiler.
- The first time it opens, JProfiler enters Setup. Click Next.
Click Next to Evaluate for 10 days.
Optionally: Click “[Select an IDE]” if you have one and click Integrate.
An example of IDE configuration is http://www.andrejkoelewijn.com/blog/
Click Next, Finish.
JProfiler is started up automatically.
- Check “Don’t show this dialog again” and Cancel.
See Sample App Leak Memory
Just to walk through JProfiler’s UI, let’s look at a program provided by JProfiler to behave badly.
Click to select the “Animated Bezier Curve demo” session which JProfile provides to intentionally leak memory not garbage collected: Its code contains a “LeakMap” object.
- Click Start.
If an Evaluation version dialog appears, click “Evaluate”.
The Session Starup settings dialog appears.
Help and Docs
Click the Help button for context-sensitive help.
PROTIP: This is the best way to access specific documentation. So if you want to review documentation on another process, open the 303 page pdf JProfiler Manual at
http://resources.ej-technologies.com/jprofiler/help/doc/help.pdf. It presents same content as Online Help.
To continue working, click on the app dialog partially hidden by the Help dialog.
Among videos created by Ingo Kegel (CTO of the company) is this from 2012 for an older version of the program.
TODO: Recreate video using new verion of JProfiler, and run Scala.
A terminal window is opened for the demo process and the main window of JProfiler displays profiling metrics.
PROTIP: Probes collect measurements.
Click OK to accept the Startup Settings.
A run is begun automatically.
Start and Stop GUI
PROTIP: In the Terminal window opened automatically, notice “JVMTI version 1.1 detected” refers to the technology the JVM provides to enable JProfiler to obtain instrumentation data.
Click on Session Settings at the top menu:
Notice we are launching a new JVM instance and using the JVM indicated referencing the relative CLASSPATH indicated.
Session with Attach
Alternately, blog entry How to monitor Kafka apps with JProfiler shows this setting screen for attaching a running program:
The app is started with this command
java -cp `mapr classpath`:nyse/nyse-taq-streaming-1.0-jar-with-dependencies.jar -agentpath:/home/mapr/jprofiler9/bin/linux-x64/libjprofilerti.so=port=11002 com.example.Run consumer /user/iandow/mystream:mytopic
It’s “jpenable” which loads the profiling agent and makes it possible to connect with a remote session from another computer.
PROTIP: The profiling agent and JProfiler GUI communicate with each other through a socket. By default, the profiling agent listens on port 8849. Many networks block traffic from such a port.
This video shows how to setup a SSH Tunnel.
Click OK to dismiss the dialog.
Watch it run
Click on Telemetries category Overview.
Among processes, click on the Demo to expose the app under test.
Check “Leak Memory” on the BezierAnim app window.
Programs that leak memory will show an upward trend in thread memory usage.
PROTIP: Many memory leaks accumulate gradually over time, so may require a long run to identify conclusively.
Click the zoom in and out icons at the lower right corner.
Each GC Activity spike reduces the amount of Memory used (shown in blue) as a portion of memory allocated (shown as the top of the green block).
Click on Live memory category All Objects view to sort objects by the amount each uses memory.
Diffs in memory usage
PROTIP: Most items are generic objects such as “java.lang.Long” which uses a lot of memory because many objects use it. We want to see the objects creating additional memory usage over time.
Click the Mark Current button at the right of the top ribbon to set the baseline for differences to be displayed.
Righ-Click on the high-level object defined by a custom name (“java.awt.geom.GeneralPath”) to select “Show selection in Heap Walker”.
“Take Heap Snapshot for Selection”.
Click OK to the Heap Snapshot Options with “Select recorded objects”.
Click “References” to activate the reference graph view.
- Select “Cumulated outgoing references”.
Click the gray arrows to expand items at each level.
Right-click on object to select “Show paths to GC Root”.
Click OK to see Single Root.
Scroll horizontally to the left in the chain.
Click on “Biggest Objects” icon.
Expand the tree by clicking gray arrows under the app “bezier.BezierAdmin”
Show source code
- Right-click to select “Use Retain Items”.
Check the “Classes” radio button, then OK.
BLAH: Where is this in v9.2?
Check the “Allocations” radio button.
QUESTION: How do we know leakMap is within this class?
- Select “Cumulated allocation tree”.
- Click OK.
Right-click to select “Show Source”.
PROTIP: Snapshots can be taken only while the program is running.
Click the Take Snapshot camera icon on the ribbon.
click the HPROF heap snapshot.
Click Save Snapshot.
Use the integration wizard to activate this by appending to the -agentpath VM parameter
Memory leaks was a small part of all issues according to this report in 2015
JProfiler Origin Tracker
Although Chrome announced it is doing away with Extensions, JProfiler Origin Tracker Chrome Browser Extension can still be useful.
PROTIP: Take a “top-down” approach to analyzing a Java program.
Ideally, you can attach JProfiler to a running server.
It can be time consuming to create load scripts that provide the coverage of code needed. So it is often useful to see running production servers.
Begin using JProfiler by using the “Profile at startup” mode.
Understand a programs’s major dependencies.
Identify the biggest objects.
Identify the rate of memory use.
Instrument classes by having JProfiler retransform the program.
Run JProfile in offline mode to save snapshots when you need to analyze several JVMs running at the same time. Then have an Ant program task to compare the snapshots.
The difference run-time data areas are illustrated here: From Java8 virtual machine architecture by Ranjith ramachandran
- To manage the Method Area holding classes defined in source code,
use Memory Profiling
NOTE: In Java8, metaspace setting takes the place of the MaxPermSize parameter that specifies space of the method area holding classes.
To manage the “thrashing” of PC (Program Counters) pointing to the next instruction to be executed in each thread,
use CPU profiling.
To manage the Java Stacks containing stack frames for each chain of methods in each thread “Stack Overflow” errors refer to this area,
use Thread Profiling.
Native Method Stacks per thread
To manage the Heap holding objects instantiated for use in JVM (garbage collected),
use the Heap Walker.
ReTransform to Instrument Code
Other Sample Programs
http://www.javamonamour.org/2014/01/how-to-demo-garbage-collection-jconsole.html discussed in https://www.youtube.com/watch?v=Bjv_9pKiALQ
JProfiler Social Media
The company behind JProfile, ej-technologies, is based in Munich, Germany.
There has been no activity on the @JProfiler Twitter account.
There is no JProfiler group on LinkedIn, Facebook, etc.
JMC (Java Mission Control)
Java Mission Control monitors the JVM in real-time.
Java Mission Control requires a commercial license for use in production.
JMC has an Experimental Plug-in for Eclipse.
PROTIP: JMC comes bundled with the HotSpot JVM since Oracle JDK 7 Update 40 (7u40). So in a Terminal:
Use the abbreviation for Java Mission Control to load it:
A welcome screen titled “Mission Control 5.5” appears.
Optional: Click the “A” at the upper-right corner to change font size.
Click the program icon
A JMX console appears.
Open another terminal window to bring up the app under observation:
java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder LoadAndDeadlock
NOTE: Although Oracle has built in the recording engine into the JVM run-time, it needs to be enabled in JVM start-up switches.
Oracle also provides optional tools for heap dump analysis and DTrace recording.
Java SE Suite provides Deterministic GC.
YourKit from Germany has profilers for both JVM and .NET.
Download from https://www.yourkit.com/java/profiler/download/
In https://www.yourkit.com/purchase/ license is $649 with a year of support.
Takipi is focused on Scala performance tuning. It is the only tool I know of that shows numbers associated with Scala code. It’s free up to 2 servers for Scala.
What to Look For
As a comparison, take a look at the profiler in Visual Studio for C# Xamarin code.