Java Flight Recorder (JFR) and VisualVM are profiling and monitoring tools for analyzing JVM performance, memory usage, and application behavior.

Java Flight Recorder (JFR)

JFR is a low-overhead event recorder built into the JVM (since Java 11, open source):

  # Start recording (60 seconds)
jcmd <pid> JFR.start name=myrecording duration=60s filename=recording.jfr

# Check recording status
jcmd <pid> JFR.check

# Stop and dump
jcmd <pid> JFR.stop name=myrecording filename=recording.jfr

# Start with application
java -XX:StartFlightRecording=duration=60s,filename=app.jfr -jar myapp.jar
  

Continuous Recording

  java -XX:StartFlightRecording=maxage=10m,maxsize=100m,dumponexit=true,filename=crash.jfr \
     -jar myapp.jar
  

Keeps a rolling 10-minute buffer — dumps on JVM exit (crashes, OOM).

JFR Events

JFR captures events including:

Category Events
JVM GC, class loading, thread lifecycle
Method profiling CPU time per method (sampling)
Memory Allocation rates, heap statistics
I/O File and socket read/write
Application Custom events via jdk.jfr API

Custom JFR Events

  @Name("com.example.OrderPlaced")
@Label("Order Placed")
@Category("Business")
public class OrderPlacedEvent extends Event {
    @Label("Order ID")
    public long orderId;
    @Label("Amount")
    public double amount;
}

// Usage
OrderPlacedEvent event = new OrderPlacedEvent();
event.orderId = 12345;
event.amount = 99.99;
event.commit();
  

Analyzing JFR Files

  • JDK Mission Control (JMC) — primary JFR analysis tool
  • IntelliJ IDEA — built-in JFR viewer
  • VisualVM — supports JFR files

Download JMC: included with Oracle JDK or standalone from Adoptium.

VisualVM

VisualVM is a visual tool for monitoring JVM processes — heap, threads, CPU, and profiling.

Starting VisualVM

  # Included in some JDK distributions
jvisualvm

# Or download standalone from visualvm.github.io
  

Features

Tab Purpose
Overview PID, main class, JVM arguments
Monitor CPU, heap, class loading charts (live)
Threads Live thread states, thread dumps
Sampler CPU and memory sampling (low overhead)
Profiler Detailed CPU and memory profiling (higher overhead)
Heap Dump Capture and browse heap contents

CPU Profiling

  1. Connect to running application
  2. Go to Sampler tab → CPU
  3. Click Sample CPU
  4. Run workload
  5. Click Stop — view hot methods by self-time and total-time

Memory Profiling

  1. Sampler → Memory
  2. Sample allocations
  3. Identify classes with highest allocation rate

Choosing the Right Tool

Scenario Tool
Production monitoring (low overhead) JFR
Development profiling VisualVM Sampler
Deep performance analysis VisualVM Profiler or JMC
Post-mortem crash analysis JFR with dumponexit
Heap leak investigation Heap dump + MAT

Production-Safe Profiling

JFR is designed for production — typical overhead is < 1%:

  # Safe for production — 10-minute rolling buffer
java -XX:StartFlightRecording=maxage=10m,maxsize=150m \
     -XX:FlightRecorderOptions=stackdepth=256 \
     -jar myapp.jar
  

Avoid VisualVM Profiler in production — it adds significant overhead.

Best Practices

  • Use JFR as the default profiling tool — safe for production
  • Configure rolling JFR buffers to capture data around incidents
  • Profile with realistic workloads — not idle applications
  • Compare before/after JFR recordings when evaluating optimizations
  • Use VisualVM during development for quick visual inspection