Profiling Windows Services

Netbeans ships with a robust profiler which is easy to use and minimally invasive. You don’t even have to be using Netbeans as your development environment (I use IntelliJ) or even be on the same machine running the VM you want to profile. Thus you can troubleshoot a production environment from afar. Yesterday I figured out how to profile a java process running as a Windows’ service via The Java Service Wrapper. The instructions below assume Java 5 on both Mac and the windows machine as well as Netbeans 6.5+.

To get started, launch Netbeans and select Attach Profiler from the Profile menu.

Launching NetBeans Profiler

This will bring up the Attach Wizard. Pick the target type as an Application and the attach method as being remote.

The next screen will prompt you for the remote host name as well as operating system.

Remote Machine

The next screen just confirms your settings.

Confirm Settings

On this screen you will want to Generate Remote Pack before clicking finish. Clicking the remote pack button will generate a zip file that you will deploy on your windows box. It contains two jar files and a platform specific DLL. By default Netbeans saves the remote pack to an obscure directory under /tmp. I recommend picking a location that is easier to find.

Generating Remote Pack

If you expand the pack you will see the files below. Running the bat files on windows assumes that java.exe is in your path and that JAVA_HOME is set. To profile an application with a 1.5 JVM you would use the profile-15.bat. To profile with 1.6 you would obviously use the profile-16.bat file. You use the bat files as a replacement for typing java at the command line. So to profile foo in bar.jar you would type profile-15.bat -classpath bar.jar foo.

File Structure

On Windows I expanded the zip file at the root of the C volume and changed the name of the directory from profiler-server-win to profiler. Then add the following snippet to your wrapper.conf file:"C:\profiler\lib\deployed\jdk15\windows\profilerinterface.dll"="C:\profiler\lib",5140
# Disable the pings so wrapper doesn’t kill the JVM while waiting for the profiler.

When you start the service, the service will pause waiting for the profiler to connect.

Wrapper Waiting

Click attach in Netbeans – now you can start digging through the VM!
Note: With remote profiling you cannot generate heap dumps.

Profiler in Action


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s