Java Mission Control — On OS X
Java Mission Control (or JMC) is a tool for tapping into Java tools and capture data, in order to analyze various metric and to do profiling.
On OS X it’s possible to just run ‘jmc’ on the command line and start it. However, it’s also nice to have an icon for it, isn’t it? Here’s a wrapper I created for Java Mission Control. The script auto detects your latest Java version.
Start Script Editor and create an Application with the following source code:
set javaHome to do shell script "/usr/libexec/java_home"
set jmcApp to javaHome & "/lib/missioncontrol/Java Mission Control.app"
run application jmcApp
I saved it as an Application and placed it in /Applications. I then replaced the applet.icns file inside the bundle with this file (shared via Google Drive):
https://drive.google.com/file/d/0B_7NRJWc_ISJdWlwYzQyMEdISkU/view?usp=sharing
I moved it to my Dock and it now looks like this (left).
Let’s try it out!
I created a little sample program that simply adds a heck of a lot of data to the heap.
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
final List<String> list = new ArrayList<>();
final Random r = new Random();
while (true) {
list.add(r.nextInt() + " ID");
list.addAll(list);
System.out.println("List is now " + list.size() + " elements long");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Run the program with the following JVM flags, otherwise it won’t work.
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder
I found the PID, and ran this on the command line:
$ jcmd 49804 JFR.start duration=120s filename=2min.jfr
This generates 2 minutes worth of Flight Recorder data that can be opened by Java Mission Control. Now I can inspect memory, code, threads, I/O, CPU and other general metrics.
Stay tuned for the next article where I’ll follow up on Java Mission Control and Flight Recorder.