The NetBeans Profiler is a powerful tool that provides important information about the runtime behavior of your application. While imposing relatively little overhead, the NetBeans Profiler tracks CPU performance and memory usage. This tutorial will show you how to use the Profiler with NetBeans 4.0 to:
Portions of this tutorial originally appeared in a June, 2004 Java Developer's Journal article (http://sys-con.com/story/?storyid=45081&DE=1) about JFluid, the underlying technology used by the NetBeans Profiler. All reprinted material is used by permission of the publisher of Java Developer's Journal.
This tutorial should take approximately one hour to complete.
NetBeans IDE 4.0 Quick Start Guide for Web Applications
Starting with NetBeans 4.0 or Java for the first time?
See the
NetBeans IDE 4.0 Quick Start Guide for Web Applications.
Once you have completed it, you will be ready to build on your knowledge with this
tutorial!
The NetBeans Profiler is in beta and is available as a separate download. To use this tutorial you must install the most recent version of the NetBeans Profiler. Be sure to follow the NetBeans Profiler Download & Install instructions.
Profiling a web application requires that the web server be run with the JVM that is included with the NetBeans Profiler. So to use the NetBeans Profiler with the Tomcat server a small change to the Tomcat startup script is necessary in order to set JAVA_HOME correctly. In addition, you must "expand" the JVM provided with the NetBeans Profiler by adding folders to it from a v1.4.2 JDK.
This tutorial uses a web application based on the HelloWeb project created in the NetBeans IDE 4.0 Quick Start Guide for Web Applications. The modifications for this tutorial include the addition of a servlet that handles the incoming request. It creates a Java bean and then dispatches to the JSP that creates the response. The servlet has been poorly written on purpose in order to highlight features of the NetBeans Profiler.
You get all application files from the ProfilingTutorial.zip file.
$UNZIPHOME now has web and src folders. The web folder contains the two JSP files and the deployment descriptors. The src folder contains the source files for the servlet and Java bean classes.
$ ps -eaf | grep java gs 8132 8104 0 12:49:32 pts/4 0:51 /export/home/gs/tools/java/jdk50_01/bin/java -Djdk.home=/export/home/gs/tools/j gs 8104 8101 0 12:49:31 pts/4 0:00 /bin/sh ./../platform4/lib/nbexec --jdkhome /export/home/gs/tools/java/jdk50_01 gs 8215 8093 0 12:50:59 pts/4 0:00 grep java
On Windows systems you can find out the process IDs by starting
the Windows Task Manager application and displaying the Processes in Image Name order. Note
that on some versions of Windows the Task Manager does not display the process
ID column (PID) by default; choose View > Select Columns and click PID to select the
process ID column. The illustration below shows that only one instance of java.exe is
currently running, with a process ID of 2848.



Now that you have verified that Tomcat is using the JVM included with the NetBeans Profiler you can attach the IDE's profiling tools to that JVM and monitor its runtime behavior.
C:\Program Files\netbeans-4.0\nb4.0\jakarta-tomcat-5.0.28\binAnd on Unix the value will be something like:
/export/home/gs/tools/netbeans/40/nb4.0/jakarta-tomcat-5.0.28/bin
The NetBeans Profiler displays three graphs in the output pane, as illustrated below.
The graph on the left is the easiest to understand. The red shading indicates the allocated size of the JVM heap, updated approximately once per second. The purple overlay indicates the amount of heap space actually in use. In the example above the allocated heap size at the last update was almost 14 megabytes. Of that 14 megabytes a little over 4 megabytes is actually being used to hold Java objects.
The graph on the right is also easy to understand. It merely shows the count of active threads in the JVM, updated approximately once per second.
The graph in the center is the most interesting. It shows two important heap statistics.
Clicking the
button will
cause the IDE to display larger versions of the three graphs in the main display pane.
The Profiler is attached to the Tomcat JVM, but is only monitoring high level statistics. To find out detailed information about the performance of a specific method (or methods) in the application you will need to modify the Profiler's settings.
button or
choose Profile > Modify Profiling.
Now that you have chosen processRequest as a root method you need to use the portion of the web application that causes that root method to run. This is easy to do because the processRequest method handles all requests from the index.jsp page.
button or
choose Profile > Get Current Results.The Profiler displays the latest performance results, as illustrated below.
The top window shows the complete method call graph beginning with the root method. The processRequest method ran for 168 milliseconds (ms). Note, however, that very little time was spent running the instructions of the processRequest method itself - the sixth line in the window shows the "self time" for processRequest to be only 0.421 ms. The vast majority of the time was spent in methods called by processRequest. In particular, the forward method took up 84.7% of the execution time. This is not surprising given the amount of work the forward method has been given to do.
The power of the NetBeans Profiler is it helps you identify bottlenecks in your code that were not expected or that will prevent your application from scaling well. Note that the createUniqueID method took up 11.3% of the execution time. You can click the plus sign next to the createUniqueID entry to investigate exactly where the time went. If you examine the source code for createUniqueID you will discover it uses a hideously inefficient algorithm that should be redesigned.
The bottom window is a flatter depiction - it shows the "self time" for each method in the call graph.
The Profiler can do detailed analysis (also referred to as instrumentation) of CPU performance or memory usage, but it cannot do both at the same time. To find out detailed information about the allocation and garbage collection of objects on the JVM's heap you will need to modify the Profiler's settings.
button
or choose Profile > Modify Profiling.Now that you have chosen analyze memory usage you need to use the web application in order to determine whether it is using memory efficiently.
button or
choose Profile > Get Current Results.
The illustration below shows the statistics for the org.me.hello.NameHandler class.
The columns provide object allocation and memory usage information.
You can use this information to help track down memory leaks. The value for Generations is the same as the Surviving Generations value described in the Interpreting the Monitor Graphs section, but restricted to the objects of a single class. This finer granularity of monitoring can help you find the specific objects that are wasting heap space.
In this example, though, you do not even need Generations to see a problem with the NameHandler class. The Total alloc. obj. value is 10. The intent of the servlet code was to reuse NameHandler objects when the same user name is entered. Clearly, that is not happening. The bug is in the processRequest method: the key used to check for an entry in the Map is not the same as the key used to store the entry. The result is a pretty typical Java memory leak: an object is placed into a Map and then forgotten.
Another handy tool for understanding application behavior is to see where in your code the objects of a particular class are being allocated. This can often provide clues about why an object is still on the heap. The NetBeans Profiler will display stack traces for any class if you right-click the class's entry in the table and choose Show Allocations Stack Traces.
To end the profiling session, choose Profile > Detach.
Refer to the NetBeans Profiler help file for more information on these topics.