What is applicationDefaultJvmArgs, how does it differ from program arguments and ad-hoc JVM flags, and when is it actually applied?
answer
- List<String> of JVM (not program) args
- applies to run task AND start scripts
- -Xmx, -D system props, GC flags
- vs run.jvmArgs = local-only
- vs --args = program args to main()
basics
~10 sapplicationDefaultJvmArgs is a list of JVM options (like -Xmx512m) baked into the application. They apply both to the 'run' task and to the launched app, unlike --args which sets program arguments.
solid answer
~40 s`applicationDefaultJvmArgs` is a `List<String>` on the `application {}` extension holding default JVM options — heap sizes, system properties, GC flags — that should accompany the application wherever it runs. Crucially these are *JVM* arguments (passed to `java` before the main class), not *program* arguments (passed to `main`). They are honoured by the `run` task and embedded into the generated start scripts so a distributed app launches with the same defaults. This is the durable, version-controlled way to ship tuning. By contrast, `run.jvmArgs` set on the `JavaExec` task only affect local `run` invocations, and `--args` on the CLI forwards program arguments to `main`. A common pattern: `applicationDefaultJvmArgs = listOf("-Xmx256m", "-Dfile.encoding=UTF-8")`.
code
kotlin · 9 linesapplication {
mainClass = "com.example.App"
applicationDefaultJvmArgs = listOf("-Xmx256m", "-Dfile.encoding=UTF-8")
}
// local-only extras, not shipped:
tasks.named<JavaExec>("run") {
jvmArgs("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005")
}go deeper
Recognize it sets default JVM flags like -Xmx and is separate from program args.
Clearly distinguish JVM args vs program args vs task-level jvmArgs and know it propagates to both run and start scripts.
Explain it as the single source of baseline JVM tuning that keeps local and distributed launches consistent; reason about local-only overrides.
Discuss governance of JVM tuning defaults across services and how baking them into the build keeps environments reproducible.
## Three distinct kinds of arguments When you launch a Java program the command looks like: ``` java [JVM args] -cp <classpath> com.example.Main [program args] ``` The application plugin gives you three knobs that map to different positions: 1. **`applicationDefaultJvmArgs`** — JVM args that travel *with the application*. They go to the left of the main class. 2. **program arguments** — passed to `main(String[])`, set on the CLI via `--args`. 3. **task-level `jvmArgs`** — JVM args set directly on the `run` `JavaExec` task; local-only. ## applicationDefaultJvmArgs in detail It is a `List<String>` property on the `application` extension: ```kotlin application { mainClass = "com.example.App" applicationDefaultJvmArgs = listOf( "-Xmx256m", "-XX:+UseG1GC", "-Dfile.encoding=UTF-8" ) } ``` What makes it special is *where it is applied*: - The **`run`** task inherits these args, so local runs match production defaults. - The plugin templates them into the **start scripts** it generates, so an installed/distributed app launches with the identical flags. (The start-script generation itself is the neighbouring concern; what matters here is that these defaults are the values that get embedded.) So it is the one place to declare baseline JVM tuning that should be true everywhere. ## Contrast with run.jvmArgs ```kotlin tasks.named<JavaExec>("run") { jvmArgs("-Dlocal.debug=true") } ``` This only affects the `run` task on a developer's machine — it is *not* baked into the application. Use it for local-only concerns (remote debug agents, extra logging) you do not want shipped. ## Common pitfall: system properties vs program args `-Dkey=value` is a JVM arg (a system property), so it belongs in `applicationDefaultJvmArgs`, *not* in `--args`. Putting `-Dkey=value` after the main class makes it a plain program argument string your code must parse itself.
- Where do -Dsystem.property flags belong — applicationDefaultJvmArgs or --args?In applicationDefaultJvmArgs (or run.jvmArgs). They are JVM args that set system properties before main runs. Putting them after the main class via --args makes them ordinary program-argument strings your code would have to parse.
- If you set both applicationDefaultJvmArgs and run.jvmArgs, what does the run task use?The run task receives the defaults plus whatever you add via jvmArgs on the task — they accumulate. Only the applicationDefaultJvmArgs propagate to the shipped start scripts; the task-level ones stay local.
saying these in an interview costs you the question
- Confusing applicationDefaultJvmArgs (JVM args) with --args (program args).
- Claiming run.jvmArgs are embedded in the distribution — they are local-only.
- Saying -Xmx should go through --args.