This article is a modification of the Getting Started With NetBeans Profiler 8.1 guide describing how to connect and profile an already running Java process. See profiler documentation for details on configuring and running the profiler.
The profiler is able to connect to any local process running on Java 6+ and started by the same user who runs the NetBeans IDE, without any additional configuration of that process. When running on Microsoft Windows, the profiled process must not be a Windows Service and must run the same Java architecture as the NetBeans IDE (32bit→32bit, 64bit→64bit).
- 1. Get The Profiler:
- Download the NetBeans IDE 8.1 distribution of your choice (Java SE technology is a requirement to get the profiler) and install or unzip it to your system. Create a sample Java application Anagram Game using File | New Project... | Samples | Java.
- 2. Start Sample Application:
- Select the AnagramGame project node in Projects window and invoke the Run Project (AnagramGame) action either in the Run menu or the IDE toolbar. The action can also be invoked using the F6 shortcut. The project is started and Anagrams window opens. That's the running process to be profiled in the following steps.
- 3. Open Profiler Window:
- Select the AnagramGame project node in Projects window and create a profiling session for this project by invoking the Attach to Project (AnagramGame) action either in the main IDE menu or the IDE toolbar. Profiler window representing new profiling session opens in the editor area. When profiling a process without the corresponding project, the Attach to External Process action needs to be used.
- 4. Configure Profiling Session:
- Click the Configure Session button in the Profiler window toolbar and select the profiler mode of your choice (for example Methods). The Profiler window displays a hint for each profiling mode to help you select the right one. Once a mode is selected, the Profiler window starts displaying its actions in toolbar and results view in the rest of the area. See Profiling Methods docs for details on profiling application performance.
- 5. Select The Process:
- Click the Attach button dropdown arrow and select the Setup Attach to Project... item to open the Attach Settings dialog. This dialog shows a list of all locally running Java processes available for profiling. The list of processes can be refreshed at any time using the Reload processes button at the bottom of the dialog. Selecting a process instructs the profiler to connect to this process each time the Attach button in Profiler window toolbar is pressed. In case the Always connect to <process> option is selected, the list shows just the processes with the selected class name. In case there's exactly one such process available for profiling, the profiler will attach to it automatically even if its process ID differs from the selected one. See Profiling Local Running Process docs for details on selecting the process to be profiled.
- 6. Start Collecting Data:
- Start the profiling session by clicking the Attach button in the Profiler window toolbar. This action connects the profiler to the running process and starts profiling it. Based on the profiler mode selected in step 6. you'll see the profiling data being collected. In case the process to be profiled isn't set up correctly, the Attach Settings dialog described in the previous step is displayed and you have to select the right process for profiling.
- 7. Switch Profiler Mode:
- Modify the profiling mode on the fly while the profiled application is running by clicking the Attach button dropdown arrow and selecting a different mode from its context menu (for example Objects). The mode is changed immediately after selecting it and the Profiler window starts displaying new data. See Profiling Objects docs for details on profiling application memory usage.
- 8. Finish Profiling Session:
- Finish the profiling session by clicking the Finish profiler session button in the Profiler window toolbar. This action detaches the profiler from the profiled process without terminating it and stops getting the results. You can also terminate the profiled process by closing its window to stop the profiling session. In this case you'll be given a chance to take a snapshot of the profiling data collected so far (not possible once the session has terminated).
- 9. Restore Profiling Session:
- Close the Profiler window and/or restart the NetBeans IDE. Create a new profiling session for the Anagram Game project or External Process and note that all the settings have been persisted for the project/process from the previous session, including the attach settings. If the previously selected process to attach is still running, it will be automatically reused for the new profiling session.
Congratulations, you've just learned how to profile an already running Java process with the new NetBeans profiler!