Reference: Clojure CLI JVM Options

Java Virtual Machine options can be passed using the Clojure CLI, either via the -J command line flag or :jvm-opts in a deps.edn alias.

Java Virtual Machine configuration and reporting

Java Virtual Machine section covers commonly used options, reporting JVM metrics and optimisation of the JVM process.

Clojure CLI command line options

Clojure CLI -J flag passes configuration options to the JVM. When there are multiple, each must be prefixed with -J.

clojure -J-XX:+UnlockDiagnosticVMOptions -J‑XX:NativeMemoryTracking=summary -J‑XX:+PrintNMTStatistics

Clojure CLI deps.edn configuration

:jvm-opts key in an alias adds JVM options to Clojure CLI deps.edn configuration. The :jvm-opts key has a value that is a collection of string JVM options ["-Xms2048m" "-Xmx4096"]

Alias to set a large heap size

:jvm/heap-max-2g {:jvm-opts ["-Xmx2G"]}

Report a full breakdown of the HotSpot JVMs memory usage upon exit using the following option combination:

:jvm/report {:jvm-opts ["-XX:+UnlockDiagnosticVMOptions"

Add a Java module

:jvm/xml-bind {:jvm-opts ["–add-modules java.xml.bind"]}

Ignoring unrecognised options

:jvm-opts ["-XX:+IgnoreUnrecognizedVMOptions"]

The aliases can be used with the Clojure CLI execution options: -A (for built-in REPL invocation), -X (for function execution), or -M (for clojure.main execution).

-J JVM options specified on the command line are concatenated after the alias options

Calling A Clojure Uberjar

JVM options must be specified when calling an uberjar with the Java command, as the project deps.edn file is not used by Java.

java -jar project-uberjar.jar -J...

Specify options or system properties to set up the Clojure service

-Dclojure.compiler.disable-locals-clearing=true - make more info available to debuggers - print stack traces to standard error instead of saving to file, useful if process failing on startup

-Dclojure.spec.skip-macros=false - skip spec checks against macro forms

Memory Management

-XX:CompressedClassSpaceSize=3G - prevent a specific type of OOMs

-XX:MaxJavaStackTraceDepth=1000000 - prevents trivial Stack Overflow errors

-Xmx24G - set high maximum heap, preventing certain types of Out Of Memory errors (ideally high memory usage should be profiled if cause not known)

-Xss6144k - increase stack size x6 to prevent Stack Overflow errors

The current default can be found with java -XX:+PrintFlagsFinal -version 2>/dev/null | grep "intx ThreadStackSize"

-Xms6G - Set minimum memory that is equal or greater than memory used by a running REPL, to improve performance

-Xmx1G - limit maximum heap allocation so a process can never use more memory, useful for environments with limited memory resources

:jvm/mem-max1g {:jvm-opts ["-Xmx1G"]}

Stack traces

-XX:+TieredCompilation - enable tiered compilation to support accurate bench-marking (increases startup time)

-XX:-OmitStackTraceInFastThrow - don't elide stack traces

Startup options

-Xverify:none option reduces startup time of the JVM by skipping verification process


The verification process is a valuable check, especially for code that has not been run before. So the code should be run through the verification process before deploying to production.

Benchmark options

Enable various optimizations, for guaranteeing accurate benchmarking (at the cost of slower startup):


-Djava.awt.headless=true - disable all UI features for disabling the clipboard for personal security:

-Dapple.awt.UIElement=true - remove icon from the MacOSX Dock - ?

Garbage Collection

Setup GC with short STW pauses which can be relevant for very high web server workloads

{:jvm-opts ["-XX:+UseG1GC"

View JVM options of a running JVM process

Use a JMX client, e.g. VisualVM

jcmd pid VM.system_properties or jcmd pid VM.flags using jcmd -l to get the pid of the JVM process

On Linux ps -ef | grep java which includes the options to run the JVM process, ps -auxww to show long arguments


results matching ""

    No results matching ""