Profiling JavaFX Applications in NetBeans IDE
- Skill Level Intermediate
- Supported Versions JavaFX 1.3
- Key Features Performance
- Last Updated February 2010
In this article, you learn how to profile a JavaFX application in NetBeans IDE to optimize the application's speed and memory footprint and ultimately to improve its performance. You can obtain data on method timing, object allocation, and garbage collection. At the moment, you can view only raw Java data types and not JavaFX data types.
As an example of a JavaFX application, you can use the Path Animation sample from http://javafx.com/samples. Download its source code from the Path Animation sample page and save it on your computer.
Selecting a Profiling Task
The NetBeans profiler enables you to perform one of the following tasks:
- Monitoring application - High level data on thread activity and object allocations
- CPU Profiling - Detailed information about application performance such as time spent on methods execution
- Memory Profiling - Object allocation and garbage collection
Each task is preconfigured with the default parameters. You can always reconfigure the profiling session to better suit your needs or to obtain some specific results. For more details about each task, refer to the following sections.
Monitoring Application
- Choose Profile > Profile Main Project from the main menu.
- Click the Monitor button on the left-hand navigation bar.
- Select the Enable threads monitoring check box if you want to monitor thread activity from the application startup.
- Click the Run button.
Figure 1 Selecting Monitoring Task
When you start monitoring, you are able to view the thread actiivity as shown in the following figure.
Figure 2 Diagram Showing Threads Activity
If you did not select threads monitoring, you can enable it during the profiling session. Select Profile > Modify Profiling Session and then select Enable threads monitoring check box.
When monitoring your application, you can view the state of the Java Virtual Machine (JVM) in the VM Telemetry window, as shown in the following diagram.
Figure 3 A Diagram Showing JVM State
You can also monitor your application by using the following advanced options:
- Overriding global settings
- Setting a working directory
- Specifying a JVM that differs from the default JVM
- Specifying JVM parameters
To set advanced settings, create a Custom configuration and click Advanced settings.
CPU Profiling
If you are interested in obtaining detailed information about application performance such as time spent on methods execution and number of invocations, select the CPU Profiling task.
Figure 4 Selecting CPU Profiling Task
- Choose Profile > Profile Main Project from the main menu.
- Click the CPU button on the left navigation bar.
- Choose between analyzing performance of the entire application or only part of it.
To profile Part of the application, you also need to specify root methods, which identify the part of the code to be profiled.
Profiling data is then collected whenever some thread enters and leaves the root method. You can specify a single root method or multiple root methods. - Click Run.
Note: Profiling points are not available for JavaFX code at the moment. When Profiling points are available, you can use them to trigger actions, such as schedule heap dumps, when certain conditions met.
The Overhead bar denotes an estimated overhead that will be imposed when profiling your application with selected settings.
If you want to specify advanced settings, create a Custom configuration and click Advanced settings.
Figure 5 Selecting CPU Profiling Advanced Options
- Time in percentage and real time spent on method execution
- Number of method invocations
- Chains of calls that lead to method invocations
To open the Live Profiling Results, click the Live Results button in the Profiler window.
Figure 6 Live Results for CPU
Memory Profiling
If you need to analyze object allocations, select the memory profiling task.
- Choose Profile > Profile Main Project from the main menu.
- Select one of the following options:
- Record objects creation only
- Record both objects creation and garbage collection
- Optionally, you can select Record stack traces for allocations.
- Click Run.
Figure 7 Selecting Memory Profiling Task
Taking Snapshots of Obtained Results
For your convenience and for future analysis, you can save snapshots of the obtained data. When your profiling session is running, choose Profile > Take Snapshot of Collected Results from the main menu. You can save snapshots either to the project or at a selected location on your disk. Then you can analyze the snapshots without running a profiling session.
Taking Heap Dumps
For the analysis of memory leaks, you might want to generate heap dumps.
Choose Profile > Take heap dump from the main menu while the profiling session is active.
Later you can view the contents of the heap without running a profiling session. Select the heap dump in the Profiler window and click Open. In the heap viewer, switch to the Classes tab, where you can see the number of class instances and the amount of memory they occupy. If you see that a relatively small number of class instances occupies the major percentage of memory, this issue might need further inverstigation.
Attaching Profiler to Your Application
- Start the NetBeans IDE.
- Choose File > Open New Project and select the PathAnimation project.
- Right-click the PathAnimation project name in the Projects window and choose Run Project.
- Choose Profiler > Attach Profiler and select the PathAnimation project in the Attach to list.
- Click the Attach button.
- In the Attach Wizard dialog box, when prompted to select the Target Type, choose Application.
Figure 8 Attach Profiler Dialog Box
Note: You can attach the profiler to any application that is not run by the IDE. If your IDE uses the JDK 6 platform, you can attach the profiler dynamically, which enables you to start the profiling of an application that is already being run.
Related Links
- Tutorial: Introduction to Profiling Java Applications in NetBeans IDE
- Tutorial: Advanced Profiling: Theory in Practice
Irina Fedortsova
Technical Writer, Sun Microsystems