2020/03/27 10:50
阅读数 634


JSR,全称Java Flight Recorder,翻译过来就是java 飞行记录器。和飞机上的黑匣子类似,用于记录Java应用运行过程中的各种事件及数据。而后,使用相应的工具来分析产生的数据,一般使用JMC, java Mission Control。

JFR是oracle jdk的商业特性,因此需要有licence才用于商业用途。个人自己玩玩的话,无所谓了。



java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder MyApp


plan A


java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=myrecording.jfr MyApp



java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder MyApp


jcmd <jvm pid> JFR.start duration=60s filename=myrecording.jfr


jcmd <jvm pid> JFR.stop

简要介绍下两个参数, filename: jfr产生的数据记录到的文件 duration:持续监控诊断时间



初始打开文件后,首页会显示概要分析,也就是一些分析建议。 比如上述

  1. 发现内存占用已达到97.9%,建议增大内存
  2. 发现物理机上有其他进程正在运行,如果是服务器环境,建议在该计算机上只运行关键进程会更好



  1. Java应用程序,此部分主要为应用程序相关的数据,包括线程、内存对象、文件IO、方法概要分析等
  2. JVM内部,主要为垃圾回收、类加载等等
  3. 环境,jvm配置参数、系统属性等



JFR是基于事件驱动,异步收集数据,所有的事件数据首先写入当前线程的buffer中,也就是ThreadLocal,无竞争!threadlocal buffer满了之后,再写到Global buffer中,注意,此处是有竞争的;当前两处均为内存中。当global buffer满了之后会再写到本地磁盘文件中。


Understanding Data Flow

JFR collects data from the JVM (through internal APIs) and from the Java application (through the JFR APIs). This data is stored in small thread-local buffers that are flushed to a global in-memory buffer. Data in the global in-memory buffer is then written to disk. Disk write operations are expensive, so you should try to minimize them by carefully selecting the event data you enable for recording. The format of the binary recording files is very compact and efficient for applications to read and write.

There is no information overlap between the various buffers. A particular chunk of data is available either in memory or on disk, but never in both places. This has the following implications:

Data not yet flushed to a disk buffer will not be available in the event of a power failure.

A JVM crash can result in some data being available in the core file (that is, the in-memory buffer) and some in the disk buffer. JFR does not provide the capability to merge such buffers.

There may be a small delay before data collected by JFR is available to you (for example, when it has to be moved to a different buffer before it can be made visible).

The data in the recording file may not be in time sequential order as the data is collected in chunks from several thread buffers.

In some cases, the JVM drops the event order to ensure that it does not crash. Any data that cannot be written fast enough to disk is discarded. When this happens, the recording file will include information on which time period was affected. This information will also be logged to the logging facility of the JVM.

You can configure JFR to not write any data to disk. In this mode, the global buffer acts as a circular buffer and the oldest data is dropped when the buffer is full. This very low-overhead operating mode still collects all the vital data necessary for root-cause problem analysis. Because the most recent data is always available in the global buffer, it can be written to disk on demand whenever operations or surveillance systems detect a problem. However, in this mode, only the last few minutes of data is available, so it only contains the most recent events. If you need to get the full history of operation for a long period of time, use the default mode when events are written to disk regularly.

Java Flight Recorder Architecture

JFR is comprised of the following components:

JFR runtime is the recording engine inside the JVM that produces the recordings. The runtime engine itself is comprised of the following components:

The agent controls buffers, disk I/O, MBeans, and so on. This component provides a dynamic library written in C and Java code, and also provides a JVM-independent pure Java implementation.

The producers insert data into the buffers. They can collect events from the JVM and the Java application, and (through a Java API) from third-party applications.

Flight Recorder plugin for Java Mission Control (JMC) enables you to work with JFR from the JMC client, using a graphical user interface (GUI) to start, stop, and configure recordings, as well as view recording files.



  1. 在Mac OS 上,java 1.8x版的jmc启动后不可用,此问题是oracle的问题,可以使用jdk7或者jdk9及更高版本jdk来分析数据

Java Mission Control 5.5 Release Notes

Java Mission Control from JDK 1.8.0_161 frozen upon startup on Mac OS X

  1. 或者你也可以单独安装JMC JDK Mission Control (JMC) 7 Downloads
  2. 官方介绍文档
0 收藏
0 评论
0 收藏