Friday, July 2, 2010

Getting Started with Java Profiler

In this article, I'm gonna explain how to use Java Profiler.

==============
Using VisualVM
==============
Sun JDK ships with VisualVM. It's a profiler tool that is best on NetBeans platform. To use it, do the following:
1. Type this in command line to start the application.
java -Dcom.sun.management.jmxremote.port=9999 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false -jar whatever.jar
2. Run VisualVM. From command line, type jvisualvm.
3. By default all the applications that run on the JVM will be listed there, but we can also add the JXM connection by right clicking on Local and choose 'Add JMX Connection' and specify in the port. In this case, it will be 'localhost:9999'. Right click then click 'Profile'.

==============
Using NetBeans
==============
Since VisualVM is based on NetBeans platform, the NetBeans profiler is somewhat similar to the VisualVM. To use it, do the following:
1. Type this in command line to start the application.
java "-agentpath:$NETBEANS_HOME/profiler/lib/deployed/jdk16/linux
/libprofilerinterface.so=$NETBEANS_HOME/profiler/lib,5140" -jar whatever.jar
2. Open NetBeans and click 'Attach Profiler'.

=============
Using Eclipse 
=============
1. Install the TPTP plugin in Eclipse.
2. Type this in command line to start the application.
java "-agentpath:$ECLIPSE_HOME/plugins
/org.eclipse.tptp.platform.jvmti.runtime_4.6.0.v201006071900/agent_files/linux_ia32
/libJPIBootLoader.so=JPIAgent:server=enabled;CGProf" -jar whatever.jar
Using agentpath only works in TPTP version 4.6 onwards. For lower version of TPTP, refer to the official guide.
2. Click 'Profile Configuration' in the Profile icon.
3. In the 'Attach to Agent'. Go to the 'Agent' tab and click that agent.
4. Click 'Profile'

This tutorial has been tested using VisualVM for JDK 1.6.0_20, Eclipse Helios, and NetBeans 6.9 on Linux. For Windows, use the appropriate library (.so file) for Windows.

No comments:

Post a Comment