How to Profile Java Applications in Docker Containers with JFR

Transcript:

Hi friends! In this video, I will show you how to profile Java applications in Docker containers using Java Flight Recorder. Java Flight Recorder (JFR) is a low-overhead profiling and diagnostic tool built into the OpenJDK, so you don't have to install it manually. We can profile the application right from the start, attach to a running application, or observe it in JDK Mission Control.

First, let's look at how to start a JFR recording at application startup. You can take any application—I'm using Spring PetClinic. The Dockerfile includes Liberica JDK Lite and minimalistic Alpaquita Linux. The most important option to enable JFR is ‘StartFlightRecording’, where you can set parameters like duration and filename. After building and running the container, you can retrieve the recording file from the ‘tmp’ directory using docker cp. The recorded data can be analyzed in JDK Mission Control, an open-source tool.

Now, let's see how to attach to a running container and profile it. Instead of enabling JFR at startup, we can use the ‘jcmd’ tool, which is part of the JDK. However, production images usually contain only the JRE, not the full JDK. To solve this, we use ephemeral containers—temporary containers that include the JDK and attach to the running application. By running an ephemeral container with ‘pid=container’, we can use ‘jcmd’ to start a JFR recording. After profiling, we retrieve the file with docker cp without restarting the main container.

Lastly, if you want to monitor a running application in real time, JDK Mission Control allows remote profiling. We modify the Dockerfile to enable remote JVM connections using JMX-related options like ‘jmxremote.port’ and ‘jmxremote.rmi.port’. After rebuilding and running the container with these options, we connect to the JVM in Liberica Mission Control. This lets us observe real-time performance metrics.

In this video, we covered how to profile Java applications in containers using Java Flight Recorder. In a future video, we will explore profiling containerized Java apps with Async Profiler, another low-overhead profiler. Don't forget to like this video, subscribe to our channel, and see you next time!

Summary

This video explains how to profile Java applications in Docker containers using Java Flight Recorder (JFR), a built-in OpenJDK tool. It covers three profiling methods: enabling JFR at application startup, attaching to a running container using an ephemeral container with `jcmd`, and monitoring real-time performance with JDK Mission Control via remote JVM connections. Step-by-step instructions are provided for configuring Dockerfiles, retrieving profiling data, and analyzing application performance.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Jun 18, 2025
Java DTO Guide: Fix Your API Design with One Simple Pattern

This tutorial shows how to use the Data Transfer Object (DTO) pattern to transfer data between application layers. We use Java records to reduce boilerplate code and the MapStruct library that simplifies Java bean mapping.

Videos
card image
Jun 13, 2025
Downgraded Java to JDK 1.1 After 30 Years… (part 1)

How should we change Java 23 code for it to run on Java 1.1? We go line by line, removing modern features like records, sealed classes, switch expressions, var, and more. Each step reveals what breaks, how to rewrite it, and what you lose in the process. If you've ever wondered how far modern Java has drifted from its roots - this is your deep dive into that gap. This is Part 1 of the Java Downgrade Challenge, where we descend version by version until we reach Java 8. Subscribe to our channel to find out how we go even deeper - all the way down to Java 1.1. Stay tuned!

Further watching

Videos
card image
Jul 15, 2025
Java Downgrade Challenge: From JDK 8 to 1.1 (Part 2)

In Part 2 of the Java Downgrade Challenge, we continue our journey — now from Java 8 all the way to Java 1.1. No streams, no lambdas, no generics, no collections — and at one point, we even boot up Windows 98. If you thought Part 1 was painful, this one unwinds Java history line by line. By the end, the familiar Java from today will be almost gone.

Videos
card image
Jun 27, 2025
5x Smaller Java Docker Images — 2025 Optimization Guide

In this video, I’ll show you how to make your Java Docker images 5 TIMES SMALLER. You’ll see the full process, from a heavy 587MB container to a lean 116MB image, ready for production.

Videos
card image
Jun 23, 2025
How to install Liberica Native Image Kit on Windows PC

Liberica Native Image Kit is a multilingual GraalVM-based set of utilities for creating native images. This guide will help you to install it on Windows PC.