skip to content

Besides -Xmx, what other JVM flags are commonly worth putting in org.gradle.jvmargs, and what is Gradle's default if you set nothing?

level: middleimportance: nice to knowfreq 26%

answer

  1. default ~512m historically, ~1g recent
  2. override replaces, not merges
  3. MaxMetaspaceSize + HeapDumpOnOOM + HeapDumpPath
  4. -Dfile.encoding=UTF-8 for reproducibility
  5. GC flags only when measured

basics

~10 s

If unset, Gradle uses a modest default heap (historically 512m, 1g in recent versions). Beyond -Xmx, common additions are -XX:MaxMetaspaceSize, -XX:+HeapDumpOnOutOfMemoryError, -Dfile.encoding=UTF-8, and sometimes -XX:+UseParallelGC or other GC/locale/encoding flags.

solid answer

~40 s

When `org.gradle.jvmargs` is unset, Gradle applies a **conservative default** to the daemon — historically `-Xmx512m`, raised to about `1g` in recent Gradle versions, plus a default metaspace setting. Because explicit `org.gradle.jvmargs` **replaces** the whole value (it isn't merged flag-by-flag), if you override it you should re-state everything you want, including encoding. Commonly useful additions beyond `-Xmx`: `-XX:MaxMetaspaceSize` to bound the long-lived daemon's class metadata; `-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath` for diagnosis; `-Dfile.encoding=UTF-8` for deterministic encoding across machines; locale system properties; and occasionally a GC choice like `-XX:+UseParallelGC` or `-XX:+UseG1GC` when GC behavior matters for build throughput. Keep the list minimal and intentional — each flag should earn its place, and GC tuning in particular should be measured, not guessed.

code

toml · 4 lines
toml
# gradle.properties — a complete, intentional override
org.gradle.jvmargs=-Xms1g -Xmx3g -XX:MaxMetaspaceSize=512m \
  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=build/heapdumps \
  -Dfile.encoding=UTF-8

go deeper

for a junior

Know there is a default heap and that -Xmx, metaspace, encoding, and heap-dump flags can go here.

for a middle

Explain that an override replaces the default and which flags are worth including and why.

for a senior

Reason about encoding reproducibility and measured GC tuning rather than guessed flags.

for a principal

Standardize a minimal, justified flag set across repos and document why each flag is present so teams don't accrete cargo-culted args.

## The default when you set nothing Gradle supplies the daemon with default JVM args if you don't override `org.gradle.jvmargs`. That default has been **conservative** — `-Xmx512m` for a long time, bumped to roughly `1g` in recent Gradle releases — together with a metaspace setting. The exact number depends on the Gradle version, but the point is: it's intentionally small, so memory-heavy builds need an explicit override. ## Override replaces, it doesn't merge A crucial gotcha: when you set `org.gradle.jvmargs`, your value **replaces** Gradle's default value entirely; flags are not merged item-by-item with the default. So if your override is just `-Xmx3g`, you've dropped whatever encoding/metaspace defaults Gradle had. Best practice is to write a complete, self-contained list. ## Flags worth considering - `-Xmx<size>` / `-Xms<size>` — max/initial heap. Setting `-Xms` near `-Xmx` avoids resize pauses on a hot daemon. - `-XX:MaxMetaspaceSize=<size>` — bound the long-lived daemon's class metadata (guards against unbounded metaspace growth from classloader leaks). - `-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<dir>` — capture a dump on OOM for diagnosis. - `-Dfile.encoding=UTF-8` — make file encoding deterministic regardless of the OS default; important for reproducible builds across machines. - Locale/timezone system properties (e.g. `-Duser.language`, `-Duser.country`) when builds are locale-sensitive. - A GC selection like `-XX:+UseParallelGC` or `-XX:+UseG1GC` — only when you've **measured** that GC is a bottleneck; the default collector is usually fine for builds. ## Example self-contained override ```properties org.gradle.jvmargs=-Xms1g -Xmx3g -XX:MaxMetaspaceSize=512m \ -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=build/heapdumps \ -Dfile.encoding=UTF-8 ``` ## Discipline Resist flag-hoarding. Each entry should have a reason: heap/metaspace for capacity, heap-dump for diagnosis, encoding for reproducibility. GC and exotic `-XX` flags are easy to cargo-cult; add them only with before/after measurements, since a wrong GC choice can hurt build throughput rather than help.

  • You override org.gradle.jvmargs with just -Xmx3g and suddenly file encoding differs on Windows vs Linux. Why?
    Your override replaced Gradle's default args (which set encoding), so the daemon falls back to the platform default encoding. Re-add -Dfile.encoding=UTF-8 to your override for deterministic, cross-platform behavior.
  • Is adding -XX:+UseG1GC to org.gradle.jvmargs generally a good idea?
    Only if you've measured a GC bottleneck. The default collector is usually fine for builds; switching collectors without before/after measurement can hurt throughput. Treat GC flags as measured tuning, not defaults.

saying these in an interview costs you the question

  • Assuming your override is merged with Gradle's default args (it replaces them).
  • Cargo-culting GC/-XX flags without measuring their effect on build time.
  • Forgetting -Dfile.encoding and getting non-reproducible builds across OSes.

context