Profiling Java programs on OS X

Posted by chetan on May 18, 2011 in computers suck, development

Sounds easy, doesn’t it? Well it actually is quite simple but the error messages along the way can really trip you up!

My first attempt at profiling a bit of code was to use the full-boat Eclipse stack: Eclipse Test & Performance Tools Platform Project! Well, what they don’t tell you anywhere on the project page is that it’s only supported on Windows and Linux. A Mac port was started sometime around 2004 and never completed. Yeah, it’s been that long!

And so this brings us to Apple’s Shark: an extremely barebones, no-frills profiler, but, what the heck, it’s free. For the basics on using this tool with your Java app, this great post has all the details. There’s just one catch: 64-bit support. There is none. If you’re on a 64-bit stack and you try to run it, you’ll see something like the following:

$ java -agentlib:Shark -cp foo.jar
Error occurred during initialization of VM
Could not find agent library: libShark.jnilib (searched /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries:/System/Library/Java/Extensions:/Library/Java/Extensions:.)

Er, what? Now let’s see here…

$ ls -al /System/Library/Java/Extensions/libShark.jnilib 
-rwxr-xr-x 1 root wheel 50352 Oct 24  2010 /System/Library/Java/Extensions/libShark.jnilib

Well, that’s odd. But it said it looked there, right?! Well, as it turns out, the Shark JNI library only supports 32-bit JVMs. So finally, we arrive at the following:

$ java -d32 -agentlib:Shark -cp foo.jar
2011-05-18 23:05:20.756 java[84473:20f] Shark for Java enabled...

Great sucess! And now, back to work, just a little less sane…