Using Profiling Points in NetBeans IDE
NetBeans IDE includes a powerful profiling tool that can provide important information about the runtime behavior of your application. The NetBeans profiling tool easily enables you to monitor thread states, CPU performance and the memory usage of your application from within the IDE, and imposes relatively low overhead.
This document describes how to use profiling points as an alternative way of setting up and controlling the profiling session. Profiling points are similar to debugger breakpoints in that you place them directly in the source code and they can trigger behavior when hit. Once you set a profiling point it becomes part of the project until you delete it. You can open the Profiling Points window to view, modify and delete the Profiling Points in your projects.
You can use profiling points to perform the following tasks:
This document will demonstrate how to use profiling points to obtain profiling data about the Anagram Game sample application, a simple Java application that is included in the IDE. Though the Anagram Game is a very simple Java application project, you would follow the same steps to profile larger, more complex Java applications, as well as web and enterprise application projects.
Before starting this tutorial you may want to familiarize yourself with using the profiling tool by reading the following documentation.
This document assumes you have some basic knowledge of, or programming experience with, the following technologies:
Software Needed for the Tutorial
For this tutorial you need to have the following software installed on your computer:
Profiling Point Basics
This section is a general guide on how to set profiling points in a project and how to view and modify existing profiling points. There are different types of profiling points, and you choose the profiling point according to the use case. A project might have a number of different profiling points associated with it. You can choose add, delete, enable and disable profiling points according to the measurements that you want to obtain during that profiling session.
Setting a Profiling Point
The easiest way to set a profiling point is in the source code in the editor window. You can simply choose the line in the code where you want to set the profiling point and then open the New Profiling Point window. You can also set profiling points when you are configuring the options for the profiling session.
When you click Finish, an icon representing the profiling point type appears in the left margin of the source editor next to the line where you inserted the profiling point.
Enabling, Disabling and Editing Profiling Points
When no profiling session is running, you can edit, enable and disable individual profiling points in either the source editor or in the Profiling Points window. To see a list of all profiling points, open the Profiling Points window by choosing Window > Profiling > Profiling Points from the main menu. The Profiling Points window shows the current status of the profiling points.
When a profiling session is running, the Profiling Points window displays how many times each profiling point was hit and enables you to view a report of the results.
You can quickly navigate to a specific profiling point in your source code by opening the Profiling Points window and right-clicking the profiling point name and choosing Show Source.
To modify the status or the settings of a profiling point, do either of the following.
If you choose to edit a profiling point, the Customize Profiling Point dialog box opens.
Viewing Active Profiling Points in a Project
You can use profiling points when you are analyzing memory usage or application performance. When you are selecting the profiling task you can see the profiling points that are active and enabled for the profiling session.
When you click Show active Profiling Points, a dialog box opens that lists all the profiling points that are set in the project. Profiling points that are set but disabled are greyed out.
Note. If you want to enable, disable or customize the settings of a profiling point, open the Profiling Points window or locate the profiling point in the project.
Working with Profiling Points
In this section you will create a sample project and then set different profiling points in the source code. This exercise will demonstrate how to use the different profiling points.
Creating the Sample Project
In this document you will use profiling points when profiling the Anagram Game sample application. To do this you will first use the New Project wizard to create the sample application.
To create the Anagram Game application, perform the following steps.
After you set the project as the main project you can see that the name of the Anagram Game project is in bold in the Projects window. By default, when using the IDE to profile a project, the IDE will profile the main project. If no project is set as the main project the IDE will profile the project that is selected in the Projects window.
Measuring Execution Time of a Code Fragment
You use the Stopwatch profiling point to retrieve timestamps when the profiling point is hit. You can also use the Stopwatch profiling point to measure how long it takes to execute a code fragment by calculating the difference between two timestamps. You can set any number of stopwatch profiling points.
When you set a Stopwatch profiling point you choose one of the following types.
To measure the execution time of a code fragment, perform the following steps.
When you click Run, the IDE launches the Anagram Game application and starts the profiling session. If you open the Profiling Points window you can see if the Stopwatch profiling point was hit. After the profiling point is hit you can click report in the Results column of the window to open a window that displays data about the profiling point and the length of time it took to execute the code fragment between the begin and end stopwatch profiling points.
Note. If this is the first time that you are profiling a project, see Introduction to Profiling Java Applications in NetBeans IDE for details on calibration and profiler integration.
Taking a Snapshot or Heap Dump
If you want to capture profiling data at a very precise moment you can use a profiling point to take a snapshot of profiling results or of the heap (heap dump). To take a profiling results snapshot (either memory or CPU) or a heap dump, you place a Take Snapshot profiling point in the source code, choose the type of snapshot and specify where you want the files to be saved. If you do not specify a location, all snapshots will be saved in the main project folder (nbproject/private).
Note. You should exercise caution when placing a snapshot profiling point because the profiling point might be hit several hundred times if it is placed in frequently executed code.
To take a snapshot using a profiling point, perform the following steps.
When you click Run, the IDE launches the Anagram Game application and starts the profiling session. If you open the Profiling Points window you can see if the Take Snapshot profiling point was hit. If the profiling point was hit, you can click report in the Results column to open the Take Snapshot window that displays data about the profiling point. To view the snapshot, click "open snapshot" in the Take Snapshot window.
Snapshot points can be effectively paired with Reset points to produce heap deltas.
When you use a Take Snapshot profiling point, the resulting snapshots or heap dumps are automatically saved to the specified location. If you specified that the snapshots should be saved to the project folder, the saved snapshots automatically appear in the Saved Snapshots list in the Profiler window. You can open snapshots in the Saved Snapshots list by selecting the item and clicking Open.
Note. If you specify that snapshots should be saved in a location other than the project folder, the snapshots are not automatically listed in the Saved Snapshots list. To open snapshots not listed in the Saved Snapshots list, click the Load button next to the list and navigate to the location of the saved snapshot.
For more about working with snapshots and heap dumps, see the following documents.
Resetting Profiling Results
You can reset collected results (either memory or CPU) at any given precise point by placing a Reset Results profiling point in the source code.
To set a Reset Results profiling point, perform the following steps.
When you click Run, the IDE launches the Anagram Game application and starts the profiling session. If you open the Profiling Points window you can see if the Reset Results profiling point was hit. If the profiling point was hit, you can click report in the Results column to open a window displaying data about the profiling point.
This document demonstrated the basics of how to use profiling points when profiling a simple NetBeans project. The steps outlined above can be applied when profiling most projects. For more detailed information about profiling settings and features not covered in this document, please consult the documentation included in the IDE and available from the Help menu item.
For related documentation, see the following resources: