← Back to Pull requests

Add an opt-in CDS archive of the dependency JARs to mn:run #1736

Merged
alvarosanchez merged 39 files (2894 lines) on Oct 03, 2026 07:46 UTC
View on GitHub
alvarosanchez alvarosanchez commented on Oct 02, 2026 21:50 UTC
Author

Motivation

Every launch and restart of mn:run parses, verifies and links all the classes of the dependency JARs again. A static CDS archive of those JARs removes that work, but the JDK refuses an archive whose class path holds a non-empty directory, and an archive of the JARs alone is only used when the runtime class path starts with exactly those JARs. Today mn:run puts the reactor's target/classes directories first, so no application archive can work. #1731 has the details and the study's measurements.

Fixes #1731

What changed

Everything is behind a new mn:run parameter, mn.classDataSharing (@Experimental, @since 5.1.0, default false). With it off, buildRunArguments returns the same list as before (pinned by a new RunMojoTest). All the logic is in the package-private ClassDataSharingSupport, next to NativeImageAgentSupport; RunMojo only calls it.

With the option on (JDK 25 or later):

  • Class path order. The regular .jar files of the resolved class path, in today's order, then the reactor outputs (target/classes of the runnable project and of the reactor modules it depends on), then any other dependency entry. Only the JARs go into the archive, so editing the project never invalidates it. Every launch uses this order, with or without an archive.

  • Duplicate check, on every launch. Each file of the entries that move after the JARs is looked up in an index of the JARs' entries, built once per key and kept next to the archive (<key>.entries). On a match (a class, logback.xml, application.yml, ...), one warning names the path and the JAR, and the launch keeps today's order and gets no CDS flag. Exempt: META-INF/services/, META-INF/micronaut/, META-INF/MANIFEST.MF, module-info.class, META-INF/versions/, and, as in micronaut-projects/micronaut-gradle-plugin#1387, META-INF/maven/, signature files and licence/notice files.

  • Recording on the developer's own launch. When the key has no archive and no class list, the launch adds -XX:DumpLoadedClassList=<key>.recording. The list is kept (trimmed to its last complete line, since destroy() is forcible on Windows) when the plugin stopped the application (restart or end of the goal), or when it exited with 0 or 130 (Ctrl+C). Any other exit status discards it.

  • Dump off the restart path. A kept list is dumped in a background thread: java -Xshare:dump -XX:SharedClassListFile=… -XX:SharedArchiveFile=<tmp> --add-modules=<root modules> <-XX: and heap options of the launch> -cp <JARs>, with JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS and _JAVA_OPTIONS removed from its environment and its output in <key>.log. Then one probe, java -Xshare:on -XX:SharedArchiveFile=<tmp> --add-modules=… <module, -XX: and heap options> -cp <launch class path> -version, and an atomic move to <key>.jsa. A failed dump or probe deletes the file, writes <key>.failed and logs one line that names the log; that key then launches without an archive. Launches during the dump run without the archive and record nothing. A successful dump deletes the files of other keys.

  • Root modules come from the final launch arguments plus the three environment variables: jdk.management.agent for any -Dcom.sun.management… property (today's -Dcom.sun.management.jmxremote, a forwarded user property or mn.jvmArgs), java.instrument for -javaagent:, jdk.jfr for -XX:StartFlightRecording or -XX:FlightRecorderOptions (also in their -XX:Option:value form; the dump still gets neither option, so it starts no recording), every --add-modules, and jdk.internal.vm.ci when the launch JVM enables JVMCI (GraalVM does by default; read before the dump with java <the launch's -XX: options> -XX:+PrintFlagsFinal -version). So the archive stays right before and after #1732.

  • Launch with the archive. -XX:SharedArchiveFile=<key>.jsa -Xlog:cds=off,aot=off right after java, before mn.jvmArgs, so a user's own -Xlog wins. Default -Xshare:auto, no probe on this path (the debug log says so).

  • Ctrl+C and -Dmn.watch=false. The shutdown hook now marks Maven as shutting down before it stops the application: a recording launch keeps its trimmed list, no dump starts and a running dump is killed and its temporary file deleted, so the exit is not delayed. The next goal dumps that list in the background, and its first launch runs without the archive. When the goal ends on its own (-Dmn.watch=false), it waits for the dump, so the goal leaves <key>.jsa behind.

  • Key (recomputed before every launch, one stat per JAR): java.home and java.vm.version of the launch java (the toolchain's when one is configured; read once per goal with -XshowSettings:properties -version), the root modules, the plain mode, the module options, the -XX:, -Xmx/-Xms and --enable-preview options, and the path, size and mtime of every JAR in order.

  • Hands off (today's command line, a debug line saying why): JDK older than 25 (one info line per goal), any of -Xshare:, -XX:SharedArchiveFile, -XX:SharedClassListFile, -XX:ArchiveClassesAtExit, -XX:+AutoCreateSharedArchive, -XX:DumpLoadedClassList, -XX:AOTCache, -XX:AOTCacheOutput, -XX:AOTMode, -XX:AOTConfiguration, -XX:+AOTClassLinking, --limit-modules, --upgrade-module-path, --patch-module, --module-path/-p, an @argfile, -XX:VMOptionsFile or -XX:Flags, in mn.jvmArgs or the environment.

  • Location. target/mn-cds/, which the watches already exclude, never attached or declared as an output. mvn clean resets it.

  • Docs. A "Class data sharing (experimental)" section in run.adoc, after "Debugging": what it does and costs, the class path order and the duplicate check, the bean and service-provider order caveat, how to check that the archive is used, when it has no effect.
  • Choices where I filled in the issue

    1. The dump also gets the launch's -XX: and heap options, not only --add-modules. Checked on JDK 25.0.4.1 with hello-netty's JARs: an archive dumped with the default heap is refused with -Xmx64g ("Unable to map shared spaces", no class from any archive, the probe exits 1), and one dumped with -XX:+UseZGC is refused with G1. Dumped with the launch's options, both pass the probe, and the -Xmx64g launch loads 1,868 classes from the archive. The functional test runs once with -XX:-UseCompressedOops to pin this.
    2. The dump and probe failure line is at info level, once per key, naming the log file (the issue said debug for the probe). A user who turned the option on otherwise has no way to see why it never helps.
    3. Option files hand off too (@argfile, -XX:VMOptionsFile, -XX:Flags): the plugin cannot see the options inside them.
    4. The exempt list is the union of both issues (Maven's plus Gradle's META-INF/maven/, signatures and licence files).
    5.
    JVMCI's module. GraalVM enables JVMCI by default, and HotSpot then adds jdk.internal.vm.ci to the root modules, but a dump always runs without JVMCI (-Xint). Without the module in the dump, every launch on GraalVM logged Mismatched values for property jdk.module.addmods: runtime jdk.internal.vm.ci,jdk.management.agent dump time jdk.management.agent and disabled optimized module handling (silenced, but a loss). The functional test caught it on the CI's GraalVM CE 25; fixed in c9990f9f.
    6.
    Parameter name mn.classDataSharing, the word of micronaut-projects/micronaut-gradle-plugin#1387's runClassDataSharing.
    7.
    JFR's module. -XX:StartFlightRecording and -XX:FlightRecorderOptions add jdk.jfr to the root modules. Before 24179c71 the dump got neither the options nor the module, and a launch with an archive and either option logged Mismatched values for property jdk.module.addmods: runtime jdk.jfr,jdk.management.agent dump time jdk.management.agent and disabled optimized module handling (checked on JDK 25.0.4.1; the classes still came from the archive). The dump now adds the module but still not the options. -agentlib:jdwp adds no module.
    8.
    -XX:+AOTClassLinking hands off instead of going to the dump (24179c71). Forwarded, it made the plain key's archive AOT-linked: the dump and the -Xshare:on probe pass, but a later -Dmn.debug launch, whose JDWP agent is not part of the key, logs shared archive file has aot-linked classes. It cannot be used with JDWP agent and loads no class from any archive, not even the JDK's default one (checked on JDK 25.0.4.1). AOT linking stays the separate opt-in of the follow-up below.

    Verification

    Unit tests (ClassDataSharingSupportTest, 51 tests, and RunMojoTest, 5):

  • with the option off, buildRunArguments is today's list; with it on, the JARs come first and the flag comes right after java;

  • order: JARs, reactor outputs, other entries;

  • a duplicate class, a duplicate logback.xml and a duplicate in a non-JAR entry fall back to today's list with a warning; a shared META-INF/micronaut/<type>/ directory and the other exempt files do not; a damaged entry index is rebuilt;

  • the key changes with a JAR's size, its mtime alone, the JDK's home and build, the root modules, -Xmx and a module option;

  • jdk.management.agent from any -Dcom.sun.management…, java.instrument from -javaagent:, jdk.jfr from -XX:StartFlightRecording and -XX:FlightRecorderOptions (also with :), also from JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS; the JFR options stay out of the dump's options;

  • status 0, 130 and a stop by the plugin keep the (trimmed) class list, 1 and 143 on its own discard it, and so does a list without a complete line; a list kept on shutdown starts no dump and the next goal dumps it; a restart through RunMojo.killProcess keeps the list and starts the dump, and the shutdown hook keeps it without starting one; a running dump is stopped on shutdown without a failure marker or temporary file;

  • a launch prepared while the recording one still runs, or during a dump, records nothing; a recording launch that failed on its own is recorded again; a failed key launches without an archive; an unreadable java or a class path without JARs keeps today's list;

  • below JDK 25: no flag, no aot tag, today's order, one info line;

  • every CDS, AOT-cache (-XX:+AOTClassLinking included), module-graph and option-file option (in mn.jvmArgs or the environment) keeps today's list;

  • a functional test with the test JDK: real recording launch, background dump and probe, a launch during the dump with no flag, then a launch with the archive and no probe, where -Xlog:class+load shows the dependency class from shared objects file and the application class from its directory, and a user -Xlog:cds=warning shows no module mismatch; the archive of an older key is deleted. It runs three times: with -XX:TieredStopAtLevel=1, with -XX:-UseCompressedOops, and with -XX:FlightRecorderOptions=stackdepth=128 (no recording; its -Xlog:cds=warning check fails without jdk.jfr in the dump).
  • Mutation check (each change made alone, then reverted): dropping the jdk.management.agent derivation, the Ctrl+C status, the duplicate check, the exemptions, the dump's -XX: options, the JDK gate, the hands-off check or the mtime in the key, or moving the flags after mn.jvmArgs, each fails at least one test; so does dropping JVMCI's module, on GraalVM. The cases added in 24179c71 (jdk.jfr from both JFR options and their : form, the JFR functional run, -XX:+AOTClassLinking handing off) fail against f7b4f099. A dump started during shutdown is caught once the test waits for background work (fixed in the test).

    Invoker ITs (pass locally on Homebrew OpenJDK 25.0.4.1, the base and native-agent ones also on Oracle GraalVM 25.0.3, and all of them in this PR's CI on GraalVM CE 25, Linux and Windows, where the watch IT is skipped; against 5.1.x without this change, run-class-data-sharing, run-class-data-sharing-duplicate and the extended multi-project-multi-runnable fail):

  • run-class-data-sharing: invocation 1 records and leaves <key>.jsa (the goal waits for the dump); invocation 2 loads io.micronaut.context.DefaultBeanContext from shared objects file and the application class from target/classes; invocation 3 with -Dmn.debug -Dmn.debug.port=0 opens the debugger port and still loads from the archive; no [cds]/[aot] line in any invocation.

  • run-class-data-sharing-native-agent (GraalVM 25+): with -Dagent=true, the agent writes reachability-metadata.json and the dependency classes still come from the archive.

  • run-class-data-sharing-watch (not on Windows): watch mode; the application restarts itself by changing a resource, waits for the archive, restarts again, then sends SIGTERM to Maven. The -X log shows the recording launch, a launch without flags during the dump, and a third launch with -XX:SharedArchiveFile and "without a probe"; the dump log has exactly one -version output; the third launch's class-load log shows the archive.

  • run-class-data-sharing-duplicate: a library JAR with its own logback.xml; the warning names logback.xml and the JAR, the project's logback.xml wins, the class path keeps target/classes first and nothing is recorded.

  • multi-project-multi-runnable: a second invocation with the option; lib/target/classes and app1/target/classes come right after the last JAR, the archive is left behind, and the existing assertions hold for both invocations.

  • Unchanged and passing: run, run-aot, run-aot-jvmargs, run-native-agent, multi-project, multi-project-multi-runnable-error, issue465.
  • End to end by hand on a Maven port of benchmark-large (Micronaut platform 5.1.5, 49 JARs, 105 project classes), in its own process group like a terminal:

  • watch mode: recording launch, restart (the list of 10,476 lines is kept and dumped in the background: 53.6 MB), restart with the archive, Ctrl+C (Maven exits 130);

  • Ctrl+C on the recording launch: <key>.classlist kept, no archive and no .failed; the next goal logs "creating the CDS archive" for its first launch, creates the archive in the background (4.4 s while the application ran, under nice), and its restart uses it.
  • ./mvnw install -Dinvoker.skip=true (all unit tests, Checkstyle, Spotless) passes on OpenJDK 25.0.4.1 and GraalVM 25.0.3. SonarCloud's findings on the first push (constants named like fields, a null Optional, a temporary file in the shared temporary directory, 78% coverage on new code) are fixed in f7b4f099; its quality gate passes.

    Measurements

    Readiness of a restart with the option on against one with it off, on one sample application, with the method of the study behind #1731 and micronaut-projects/micronaut-gradle-plugin#1387.

  • Machine: Apple M4 Pro (12 CPUs, 48 GB), macOS 26.6.2, Homebrew OpenJDK 25.0.4.1. The machine was shared with other work: each session ran under a timing lock and started once the 1-minute load average was below 3 (a quarter of the CPUs); the load medians during the sessions were 4.7, 3.5 and 3.4. Read the figures as relative ones.

  • Application: a Maven port of benchmark-large (micronaut-parent 5.1.5, 49 dependency JARs, 105 project classes, 48 beans), run with this branch.

  • Launches: the exact command lines that mn:run -X logged (Running …): today's (option off) and, with the option on, a restart with the archive, the recording launch, and a launch without an archive (as during a dump). The archive came from a watch-mode session whose class list was recorded until the restart (10,476 lines, 53.6 MB archive). The harness started the commands directly, in target, so Maven's own time (compilation, configuration validation) is not in the figures.

  • Method: readiness from spawn to the first HTTP 200 on /hello; 3 sessions, each with 3 warm-ups and 15 iterations, the variants interleaved in random order (n = 45 pairs per variant); the median of the paired differences as a share of today's launch, with a bootstrap 95% CI. "3/3" means that each session's own CI excludes zero.
  • | Launch with the option on | Readiness against today's launch | Sessions |
    |---|---|---|
    | Restart with the archive |
    −42.5% [−43.1, −41.9] | 3/3 (session medians −41.8%, −42.0%, −43.1%) |
    | Recording launch (the first one per key) | +2.1% [+1.5, +3.8] | 3/3 |
    | Launch without an archive (during a dump, or for a failed key) | −1.1% [−2.4, +0.3] | no effect |

  • RSS at readiness (per-variant medians, not paired): 142 MiB with the archive against 151 MiB today (−6%); 151 MiB for the recording launch.

  • The dump and its probe took 4.4 and 5.7 s in the background in the two end-to-end runs (while the application and Maven ran, under nice; not timed under the lock).

  • This matches the study's −44.2% for benchmark-large on the run-task command line, measured outside Maven.
  • SemVer and API

    New, opt-in behaviour, so it targets 5.1.x (the next minor, 5.1.0-SNAPSHOT). The only new user-facing surface is the mn.classDataSharing parameter of mn:run (private field, @Experimental, @since 5.1.0, default false). No new public type or method; ClassDataSharingSupport is package-private. With the option off, the command line is unchanged; killProcess and the shutdown hook only gained calls that are no-ops when the option is off.

    Follow-ups

  • AOT class linking (step 8 of #1731): a second opt-in, mn.classDataSharing.aotClassLinking to match aotClassLinking of micronaut-projects/micronaut-gradle-plugin#1387, on JDK 25 and 26 only, with a plain archive for JDWP, agents, --add-opens/--add-exports, a plain fallback when the AOT-linked dump fails, and a measurement with a session-length class list.

  • JDK 21: launch as today until an integration test covers it.

  • #1732: measure this option together with dropping -Dcom.sun.management.jmxremote once both exist (the archive also holds the JMX agent's classes; the root modules follow the launch, so the archive stays correct either way).

  • mn:dev** (#1725) could reuse the helper for its JAR-only class path.

  • Not measured: Linux, Windows, JDK 26 and 27, readiness through mn:run itself (with Maven's compilation), and the cost of the duplicate check per launch.
  • 🤖 Generated with Claude Code

    Diff: 2894 lines (+2885 / -9) across 39 files Reviewer: graemerocher Review duration: 595m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.72) × (1 − Review Confidence (0.65)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.72 · 2894 lines diff across 39 files
    0.72
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.32 · 1 substantive comment(s)
    0.32
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    65%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Shallow Review Depth shallow_review_depth
    0.239
    Mass File Touch mass_file_touch
    0.080
    Reviewers
    Assignees
    alvarosanchez alvarosanchez
    Labels
    risk: low confidence: 65.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 71.5% (HIGH) · Higher = larger blast radius
    71.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 65.1% · Lower = rushed or shallow review
    65.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.9% · Safe to merge
    24.9%