Most developers use Java Flight Recorder by adding specific flags to the application startup command to capture data in the background. This method works for most production environments, but you can also start a recording on a process that is already running using a separate command line tool. Labels and menu paths may differ by version.
Prepare the Environment Before Starting
Before you trigger a recording, ensure you have the necessary permissions to attach to the process. You will need a tool to view the resulting files, such as Java Mission Control, installed on your local machine. Check that the disk where the recording will be saved has enough free space to hold the data, as the size depends on the duration and the level of detail you choose. Verify that the application is running with a compatible runtime that supports the recording features. If you are in a containerized environment, ensure the container has the rights to write files to the specified volume.
Start a Recording Using Startup Flags
- Open your application configuration or startup script.
- Add the flag to enable the recorder.
- Specify the destination file path where the recording should be saved.
- Set the recording duration to determine how long the data is collected before it wraps around.
- Define the disk repository size to limit how much space the recorder uses on the drive.
- Launch the application and let it run under the load you wish to analyze.
- Stop the application or use a command to dump the current buffer to a file.
Trigger a Recording on a Running Process
When an application is already live and you notice a performance dip, you can start a recording without restarting the service. Use the command line tool provided with the runtime to identify the process ID of your application. Once you have the ID, run the start command and specify the filename for the output. You can choose a specific profile, such as a default profile or a more detailed one, depending on whether you are looking for general trends or deep memory leaks. Let the recorder run while the issue is occurring, then use the dump command to save the data to a file for later analysis. This method is useful for capturing intermittent spikes that do not happen every time the app starts.
Analyze the Data in the Viewer
Once you have the recording file, open it in the analysis tool. Start by looking at the event timeline to see when the spikes in CPU or memory occurred. Use the method profiling view to see which specific pieces of code are consuming the most resources. You can filter the data by thread to see if a single thread is blocking others or if the work is spread evenly across the system. Look at the garbage collection events to see if the application is spending too much time reclaiming memory, which often causes pauses in responsiveness. Compare two different recordings from different time periods to see if a change in code or configuration improved the performance.
Adjust the Recording Profile for Detail
The amount of data collected depends on the profile you select. A low-overhead profile is best for production systems where you want to minimize the impact on the user experience. This profile captures the most important events but might miss some fine-grained details. A high-detail profile captures more frequent events, such as every single allocation or lock contention, but it increases the load on the CPU and fills the disk faster. If you are debugging a rare race condition, the high-detail profile is necessary. If you are monitoring general health, stick to the default settings. You can find the specific names of these profiles in the tool documentation.
Identify Issues Through Event Patterns
Look for patterns in the event stream to diagnose the root cause of a problem. If you see a high number of lock events, it means threads are fighting over the same resource, which slows down the whole system. If the memory usage climbs steadily without ever dropping, you likely have a memory leak. Check the allocation rate to see if the application is creating too many short-lived objects, which puts pressure on the garbage collector. If the CPU usage is high but the method profiling shows no single dominant method, you might be dealing with too many small tasks or external system waits. Use the I/O events to see if the application is waiting on the disk or the network, which can make a fast CPU feel slow to the user.
Fix Common Recording Failures
If the recording file is empty, check if the application had write permissions for the folder you specified. If the recording stops prematurely, the disk repository might be too small for the amount of data being generated. If you cannot attach to a running process, ensure the user running the tool is the same user that started the application. If the analysis tool fails to open the file, verify that the file was closed properly and not corrupted during a crash. When the recording overhead is too high, reduce the sampling frequency in the profile settings. If the data is too noisy, use the filter tools in the viewer to hide events that are not related to your specific problem.