GraalVM/Quarkus原生镜像性能测试:内存CPU等监控方法咨询
Great question! When you switch from traditional Java apps to Quarkus native images, you don’t have to settle for just top or ps for monitoring. There are several tools and approaches that give you the same deep insights as VisualVM—plus some native-image-specific capabilities. Let’s walk through them:
1. GraalVM Built-In Monitoring (JFR, JMX, Heap Dumps)
GraalVM lets you enable Java-style monitoring for native images at build time. This unlocks tools you’re already familiar with, like Java Flight Recorder (JFR) and JMX, which provide detailed metrics on memory, threads, CPU, and more.
How to Enable It
When building your native image, add the --enable-monitoring flag to include the features you need:
# Maven example ./mvnw package -Dnative -Dquarkus.native.additional-build-args=--enable-monitoring=heapdump,jfr,jmx # Gradle example ./gradlew build -Dquarkus.package.type=native -Dquarkus.native.additional-build-args=--enable-monitoring=heapdump,jfr,jmx
Using Java Flight Recorder (JFR)
JFR is perfect for capturing detailed runtime data during performance tests:
- Start a recording on your running native process:
jfr start --pid=<your-native-app-pid> - Run your performance test suite.
- Stop and dump the recording to a file:
jfr dump --pid=<your-native-app-pid> --filename=native-perf-recording.jfr - Open the
.jfrfile in Java Mission Control (JMC)—it’ll show you memory allocation patterns, thread activity, CPU usage breakdowns, and even native-specific events like garbage collection in the native image’s Substrate VM.
Using JMX with VisualVM/JConsole
If you enable JMX monitoring, you can connect VisualVM or JConsole directly to your native image, just like a regular Java app:
- Start your native image with JMX configuration flags:
./your-native-app -Dcom.sun.management.jmxremote.port=9090 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false - Open VisualVM, add a JMX connection using
service:jmx:rmi:///jndi/rmi://localhost:9090/jmxrmi, and you’ll see familiar tabs for memory, threads, and MBeans.
2. Quarkus-Specific Monitoring Extensions
Quarkus has built-in extensions that make it easy to expose and collect performance metrics during tests:
- Micrometer: Add the
quarkus-micrometerextension to expose metrics like heap memory, thread counts, CPU usage, and application-specific metrics. You can scrape these with Prometheus and visualize them in Grafana for real-time monitoring during performance tests. - SmallRye Health: While not a full performance monitor, it can help you track basic liveness and readiness alongside performance metrics to ensure your app stays stable under load.
3. OS-Level Tools (As a Complement)
Don’t overlook OS tools—they’re great for getting a high-level view and complementing the GraalVM-specific tools:
- Linux: Use
htopfor CPU/memory/thread overview,perffor deep CPU profiling,pmapto inspect memory mappings, andjstack(yes, it works with native images if JMX is enabled) to get thread dumps. - macOS:
Activity Monitorfor basic metrics,dtracefor low-level tracing. - Windows: Task Manager or Resource Monitor for real-time CPU/memory/thread data.
Key Notes
- Make sure you’re using the GraalVM JDK (not a standard OpenJDK) when building and monitoring native images—some tools rely on GraalVM-specific components.
- For heap dumps, you can trigger one with
jcmd <pid> GC.heap_dump heapdump.hprof(if heapdump monitoring is enabled) and analyze it with tools like Eclipse Memory Analyzer (MAT).
All these options give you the same (if not deeper) visibility than VisualVM, tailored to the unique nature of native images. Mix and match them based on what metrics you need to focus on during your performance tests!
内容的提问来源于stack exchange,提问作者codependent

