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
Apr 1, 2025
SBOMs & Java Security: Stay Compliant, Stay Protected!

A software bill of materials or SBOM is crucial for the modern development environment full of regulations, vulnerabilities, and hidden threats. In this video, we discuss what SBOMs are and how they help dealing with compliance audits and application security. We will explore the best open-source tools for generating SBOMs and provide a tutorial on creating an SBOM for your project and analyzing it for vulnerabilities.

Videos
card image
Mar 21, 2025
Async Profiler: Uncover Hidden Performance Issues in Java!

Let's discuss Async Profiler, the small and effective open-source low-overhead profiler for Hotspot JVM-based application. Itcan collect data on various JVM events like CPU usage, heap allocation, methods, and so on. It can monitor non-Java threads, native calls, and kernel functions. It is small, silent, and incredibly effective!

Further watching

Videos
card image
Apr 25, 2025
Java in 2025: Busting the Biggest Myths

Think Java is slow, outdated, or only for old-school enterprise apps? Think again. In this episode of Java Myth Busters, we debunk six common myths about Java, including performance issues, security concerns, and the myth that Java is dead. Discover how the JVM, Spring Boot, GraalVM, and modern JDK updates make Java one of the most powerful, scalable, and relevant languages in 2025.

Videos
card image
Apr 16, 2025
Spring Boot Buildpacks: Optimize, Secure, and Shrink Your Containers!

Buildpacks let you containerize Spring Boot apps without writing Dockerfiles, but the default settings aren’t always optimal. In this video, I’ll show you how to fine-tune buildpacks for faster startup, smaller images, and better security—so you get the best performance with minimal effort.

Videos
card image
Apr 1, 2025
SBOMs & Java Security: Stay Compliant, Stay Protected!

A software bill of materials or SBOM is crucial for the modern development environment full of regulations, vulnerabilities, and hidden threats. In this video, we discuss what SBOMs are and how they help dealing with compliance audits and application security. We will explore the best open-source tools for generating SBOMs and provide a tutorial on creating an SBOM for your project and analyzing it for vulnerabilities.