Profiling OSGi applications using VisualVM

Recently been into working with OSGi modules built on top of Apache Felix using the Eclipse bndtools. This ain’t fun all the time, but it works. However, I am then and now using visualvm for profiling and monitoring issues. With OSGi, at least the VisualVM profiler doesn’t work out of the box but instead causes a lot of fun just like this:

at org.mortbay.thread.QueuedThreadPool$
Caused by: java.lang.ClassNotFoundException: org.netbeans.lib.profiler.server.ProfilerRuntimeCPUFullInstr not found by org.apache.felix.http.jetty [34]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(
at org.apache.felix.framework.BundleWiringImpl.access$400(
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
... 2 more

There’s a blog post out there explaining how to set up various profilers to work with OSGi, which also includes VisualVM. A bit of playing makes this work with Eclipse and bndtools, too. Not much to add, except for that it’s important to, in Eclipse / bndtools, put the “right” things in the right place:

  • Download an up-to-date version of visualvm, see here. Unpack this to a meaningful folder.
  • In your bndrun configuration file, add -Xbootclasspath/p:/path/to/your/visualvm_136/profiler/lib/jfluid-server.jar to the -runvm section. This should make your Java VM include the jfluid-server.jar (the part of the profiler that includes the required classes) on startup.
  • In the same file, add org.osgi.framework.bootdelegation=org.netbeans.lib.profiler.* to the -runproperties section to make OSGi find the classes included in this package.

By then, you should be able to connect VisualVM to an OSGi framework running from within Eclipse / bndtools and enable CPU and memory profiling just the way you’re eventually used to.