Tuesday, April 15, 2008

I'm back with NetBeans Profiler

I had been playing around with NetBeans profiler for past few days. All I can tell is that NetBeans Profiler is one of its kind and the most gorgeous tool in NetBeans.


Getting Started
Prior to using Profiler with any project we need to set the profiler to get accurate results later and to integrate the tool with your project. To achieve accurate results profiler must be calibrated in the beginning. To do this go to Profiler-->Advanced Commands -->Run Profiler Calibration. A dialog box appears where you need to select your JDK.



The calibration needs to be run only once. However if any significant changes are made to your machines, you need to run it again. A dialog box appears after the calibration where you can check the info about calibration and it also saves the calibration information in .nbprofile in your home directory. The profiler tool profiles the main project by default, However you can select the project you want by right clicking on the required project and selecting Profile option in the pop-up menu.

The profiler tool window appears wherein you can select the required action.



To integrate with the project, the IDE modifies the build script of your project to import an additional build script for the required profiling libraries. Before modifying the build scipt, the IDE creates a backup of the original build script. You can restore the original build script by choosing Profiler > Advanced Commands > Unintegrate from the main menu.

IDE provides you different profiling tasks which you can select according to your needs. The following table describes the profiling tasks and the profiling results obtained from running the task.
Profiling Task Results
Memory Application
Choose this to obtain high-level information about properties of the target JVM, including thread activity and memory allocations.
Analyze CPU Performance
Choose this to obtain detailed data on application performance, including the time to execute methods and the number of times the method is invoked.
Analyze Memory Usage Choose this to obtain detailed data on object allocation and garbage collection.


Monitoring an Application
The target application is starts soon after you click on the above option. Note that the overhead is low in this case.



To get the Telmetric view, under the view option (in the profiler window) select VM Telemetry, It must be something like this,

The Profiler window contains the controls that enable you to do the following:
  • Control the profiling task
  • See the status of the current profiling task
  • Display profiling results
  • Manage profiling results snapshots
  • See basic telemetry statistics
Analyzing CPU Performance
This part is quite interesting! CPU task allows you to profile the entire application or a particular code fragment (Of course you can select this!). It allows method by method execution.
You can select one of the options in the window,
  • Entire Application : The IDE instruments the entire code that you have written. It highlights the thread entry and exit points as well as calculates the time spent in each method.
  • Part of type Application : In this mode you can profile a particular code snippet of your interest. This significantly reduces the profiling overhead. You need to select the root methods in order to give profiler an entry point. Profiling data is collected when one of the application's threads enters and leaves the instrumentation root. No profiling data is collected until one of the application's threads enters the root method.


After defining the required root methods the same thread window pops up showing the thread execution status as well as the time taken by the entire method to run. You can view the Signal Dispatcher (it shows the status of selected threads) in the thread window by selecting the appropriate threads in the main window of the threads. Screen Shot of my app is shown below:



Analyzing Memory Usage
This mode gives you information regarding allocation to memory to objects, type if objects etc etc.
You can select one among these :
  • Record object creation only : When this option is selected, all classes currently loaded by the target JVM (and each new class as it is loaded) are instrumented to produce information about object allocations.
  • Record both object creation and garbage collection : When selected, profiling the application gives you information on object liveness such as about how many objects of each type are still alive, their size and average age, as well as allocation data.


You can also view the live results by selecting live results under the profiler menu in profiler window.



Taking SnapShots
You can obtain the memory snapshots of your application and also see the allocation stack trace for any object you want.
To do this,
  • Make sure that the profiler is still running, otherwise go back to your project, right click on it and open profiler tool again as mentioned before.
  • Open up the Live Results from the Profiler window, right click on an object whose stack trace you want and click on take snapshot.

The IDE takes the memory snapshot and opens the snapshot to the Allocation Stack Traces tab where you can explore the reverse call tree for methods that instantiated this object.

No comments: