Java Performance Profiling with JFR and VisualVM
Jul 29, 2025 am 01:32 AMJFR and VisualVM are an efficient combination of Java performance analysis. JFR is responsible for low overhead to collect JVM running data, and VisualVM is used for visual analysis. 1. Enable JFR to enable dynamically by adding the -XX: FlightRecorder parameter at startup or using the jcmd command during operation. It is recommended to use the profile template to obtain more detailed events. 2. Use the jcmd command to set parameters such as duration and filename for timed recording, or you can accurately control the acquisition event through custom .jfc files. 3. VisualVM needs to install the community enhancement version and add the JFR plug-in. After loading the .jfr file, you can analyze CPU, memory, GC, thread and other issues through Overview, Telemetry, Garbage Collections, Memory, Threads, Code and other panels. 4. When positioning performance bottlenecks, Hot Methods is used to discover CPU hotspots, Memory panel view object allocation, Threads panel recognizes lock competition, and GC charts determine recycling frequency and pauses. 5. In actual use, the default template should be avoided, the recording time should be set reasonably, and the process should be practiced regularly to deal with online problems. Although this combination is not the latest, it is still light and effective, suitable for most Java developers to quickly diagnose performance problems.
Java performance analysis is a key step in optimizing applications and troubleshooting bottlenecks. In actual development and operation and maintenance, JFR (Java Flight Recorder) and VisualVM are two very practical tools. They can use them in combination to gain insight into the JVM running status and application behavior. Here is a detailed description of how to use them for performance analysis.

What are JFR and VisualVM?
JFR (Java Flight Recorder) is a built-in low-overhead performance monitoring and event recording tool in JDK. It can continuously collect internal JVM running data, such as GC, threads, lock competition, method sampling, etc., and is suitable for use in production environments.
VisualVM is a graphical monitoring and analysis tool that supports viewing JVM memory, CPU, threads, and class loading, and can load JFR recording files for visual analysis.

Both are used in conjunction: JFR is responsible for collecting data, and VisualVM is responsible for displaying and analyzing.
How to enable and use JFR for performance logging
JFR is available starting from JDK 8u40 and is integrated by default in JDK 11 and is free (early versions of Oracle JDK require commercial authorization).

How to start JFR record:
You can enable it dynamically via the command line or JMX:
# Method 1: Turn on JFR at startup java -XX: FlightRecorder -XX:StartFlightRecording=duration=60s,filename=app.jfr MyApplication # Method 2: Dynamically turn on during operation (assuming you know the process PID) jcmd <pid> JFR.start duration=30s filename=recording.jfr
Common parameters description:
-
duration
: recording duration (such as 60s) -
filename
: output file path -
maxage
/maxsize
: loop record settings -
settings
: custom event configuration template (such as "profile" mode for more detailed)
For example, enable a high-performance profile record:
jcmd <pid> JFR.start settings=profile duration=120s filename=perf.jfr
This profile
template enables more sampling events, suitable for in-depth performance analysis.
Analyzing JFR records using VisualVM
Although VisualVM is no longer actively maintained (archived), it is still widely used in local debug analysis. Supporting JFR is its power.
Install and configure VisualVM (third-party enhanced version is recommended)
The official original VisualVM has limited support for JFR. It is recommended to use VisualVM with JFR plugin , the "VisualVM 2.0" version maintained by the community.
Download address: http://ipnx.cn/link/81a25c4e9b7f4f8fa356e4863b32735e
Brief installation steps:
- Download and unzip VisualVM
- After startup, enter Tools → Plugins → Available Plugins
- Install the "Java Flight Recorder" plugin
- Restart takes effect
Loading and analyzing JFR files
- Open VisualVM
- Select File → Load → Select
.jfr
file in the menu bar - View the following key panels:
1. Overview
- Display recording time, JVM information, host environment and other metadata
2. Telemetry
- Real-time display of heap memory, non-heap memory, GC times, thread count, and CPU usage trend chart
- Quickly determine whether there is a memory leak or a CPU soaring problem
3. Garbage Collections
- Displays the time, type (Young GC / Full GC), front and back heap sizes of each GC
- Frequent GC or prolonged pauses can be found (STW)
4. Memory
- Heap object allocation hotspot (Allocated Objects by Type)
- Can locate large number of small objects created (such as String, HashMap)
5. Threads
- Thread state changes, reasons for blocking
- The lock competition event (Monitor Wait, Block) is clearly visible
- Synchronous blocks that can identify deadlocks or high latency
6. Code (Hot Methods)
- Method sampling call stack (based on timing sampling)
- Find out the hotspots that take up the most CPU
- Supports expanding call chains by thread, which facilitates slow positioning and operation
?? Note: JFR does not enable all events by default. If you do not see enough details, you should use
profile
configuration or custom.jfc
files to enhance sampling accuracy.
Practical skills: Quickly locate common performance problems
Question Type | JFR VisualVM Analysis Point |
---|---|
Excessive CPU usage | Check "Hot Methods" to see which methods consume the most samples; check whether infinite loops or regular match explosions |
Memory growth too fast | Check "Memory → Allocations" to find a large number of instantiated classes; use GC frequency to determine whether it is leaking |
Request response slows | Check the thread waiting event (Monitor Blocked), is there any lock competition? Database connection pool exhausted? |
GC frequently | Check the "Garbage Collection" chart to confirm the GC frequency and duration; adjust the heap size or select the appropriate GC algorithm |
For example:
You see java.util.HashMap.get()
appears at the top in "Hot Methods" and comes from a cache class. This may mean:
- Low cache hit rate, frequent hash calculation
- Or concurrent access is serious, causing the red and black tree to degenerate, further view the thread stack, confirm the call context, and then decide whether to replace it with
ConcurrentHashMap
or optimize the key design.
Tips and precautions
- Low overhead does not mean zero overhead : JFR default overhead is <2%, but it is still recommended to control the sampling frequency in extreme high throughput scenarios.
- Setting the recording time reasonably : If it is too short, you won’t catch the problem. If it is too long, it will affect performance and have large files.
- Use settings file to customize events :
jcmd <pid> JFR.check # View available templates jcmd <pid> JFR.start settings=myconfig.jfc ...
You can edit the
.jfc
file yourself and only enable events you care about (such as only focusing on threads and GC). - Do not rely on the default template for in-depth diagnosis : the default template has fewer events, so it is recommended to use
profile
or custom configuration.
Basically that's it. JFR provides powerful underlying data acquisition capabilities, and VisualVM makes these cold data intuitive and easy to understand. Although there are more modern tools (such as Async-Profiler, JMC, Prometheus Grafana), for most Java developers, this combination is still sufficient, lightweight, and requires no code entries.
What is not complicated but easy to ignore is: practice the complete recording → analysis process in advance , and can only respond quickly when there is a problem online.
The above is the detailed content of Java Performance Profiling with JFR and VisualVM. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java performance is closely related to hardware architecture, and understanding this relationship can significantly improve programming capabilities. 1) The JVM converts Java bytecode into machine instructions through JIT compilation, which is affected by the CPU architecture. 2) Memory management and garbage collection are affected by RAM and memory bus speed. 3) Cache and branch prediction optimize Java code execution. 4) Multi-threading and parallel processing improve performance on multi-core systems.

Java development is one of the most popular programming languages ??at present. Its power lies in its rich data structure and algorithm library. However, for developers who are just getting started or want to improve themselves, how to efficiently handle data structures and algorithms is still a challenge. This article will share with you my experience and suggestions in Java development, I hope it will be helpful to everyone. First, it is very important to understand common data structures and algorithms. Java has built-in many commonly used data structures and algorithms, such as arrays, linked lists, stacks, and queues.

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

How to solve: Java Performance Error: High CPU Usage When developing Java applications, you often encounter the problem of high CPU usage. This can cause application performance degradation and consume significant computing resources. This article will provide some methods to solve the problem of excessive CPU usage of Java applications, and attach code examples. Check for loops and recursions in your code In Java, loops and recursions are one of the common causes of high CPU usage. Please make sure there are no unnecessary loops and recursions in your code, and try to

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

JAVA underlying performance optimization and tuning practice summary: With the rapid development of the Internet, JAVA, as a high-performance, high-reliability programming language, is widely used in various fields. However, due to the existence of the JAVA Virtual Machine (JVM), many developers may not understand the underlying implementation and performance tuning techniques of JAVA. This article will introduce some JAVA underlying performance optimization and tuning practices to help developers better understand and take advantage of JAVA's performance advantages. 1. Understand the JAVA virtual machine and learn the underlying nature of JAVA

GC tuning optimizes Java framework performance by adjusting JVMGC parameters, including new generation size, garbage collection threshold and concurrent GC mode. In actual cases, GC tuning for the SpringBoot framework reduced the average response time by 100ms, 400ms, and 1000ms respectively, proving the significant impact of GC tuning on the performance of the Java framework.
