java - launch a Java application
java [options] -jar jarfile [args ...]
java -XX:+PrintFlagsFinal -version
Options you will use
-version- Version to stderr.
--version- Version to stdout.
-XX:+PrintFlagsFinal- Every flag with its final value and where it came from ({default} {ergonomic} {command line}).
-XshowSettings:vm|system- Estimated max heap, and the cgroup limits the JVM sees.
-Xmx<size>- Max heap. Overrides MaxRAMPercentage.
-XX:MaxRAMPercentage=<pct>- Heap as a percentage of the memory the JVM sees (the cgroup limit when UseContainerSupport is on, which is the default). Default 25.
-XX:NativeMemoryTracking=summary- Enable NMT (must be set at start).
-Xlog:gc*:file=<f>:time,uptime:filecount=5,filesize=10M- Unified GC logging to a rotating file.
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<dir>- Dump on OOM.
-XX:+ExitOnOutOfMemoryError- Exit (code 3) on the first OOM instead of limping on.
-jar JARFILE- Run the program packaged in JARFILE (its manifest names the main class). Everything after the jar goes to the program, not the JVM.
-cp, -classpath, --class-path PATH- Where to look for classes: directories and jars separated by :.
-D NAME=VALUE- Set a system property, written attached: -Dotel.service.name=orders, -Djarmode=tools (Spring Boot's jar tools mode). Must come BEFORE -jar, or it goes to the program as an argument.
-javaagent:opentelemetry-javaagent.jar- Load the OpenTelemetry Java agent (-javaagent:JAR[=OPTIONS]): it instruments the app at start, no code change. Configure it with -Dotel.* properties or OTEL_* variables.
-XshowSettings:vm- Print the VM settings and continue: the max heap it picked from the memory it sees.
-XshowSettings:system- Print the operating environment the JVM detected (Linux only): the container/cgroup CPU and memory limits.
-Xlog:os+container- Unified logging for the container detection code (=trace for everything): shows which cgroup files it read and the limits it found.
-Xlog:gc- Unified GC logging (-Xlog:gc* for all the gc tags).
-Xmx512m- Max heap 512 MB (-Xmx<size>). An explicit -Xmx overrides MaxRAMPercentage.
-Xmx1g- Max heap 1 GB (-Xmx<size>). An explicit -Xmx overrides MaxRAMPercentage.
-Xmx256m- Max heap 256 MB (-Xmx<size>).
-XX:+UseParallelGC- Use the Parallel (throughput) collector.
-XX:MinHeapFreeRatio=<pct>- Grow the heap when less than this percentage is free after a GC.
-XX:MaxHeapFreeRatio=<pct>- Shrink the heap (give memory back) when more than this percentage is free after a GC.
-XX:GCTimeRatio=N- Parallel GC throughput goal: at most 1/(1+N) of the time in GC.
-XX:AdaptiveSizePolicyWeight=N- How much the adaptive sizing weighs the current GC vs the history (0-100).
-Xms<size>- Initial heap size.
-Xss<size>- Thread stack size (default 1M on 64-bit Linux).
-XX:+UseG1GC- Use the G1 collector (the default on servers; the JVM picks Serial when it sees <2 CPUs or <1792 MB).
-XX:+UseSerialGC- Use the single-threaded Serial collector.
-XX:+UseZGC- Use ZGC: very short pauses on large heaps.
-XX:MaxRamPercentage=<pct>- WRONG CASE: flags are case sensitive, the real one is -XX:MaxRAMPercentage. The JVM fails with "Unrecognized VM option" and does not start.
-XX:InitialRAMPercentage=<pct>- Initial heap as a percentage of the memory the JVM sees.
-XX:MaxMetaspaceSize=<size>- Cap Metaspace (class metadata, unbounded by default).
-XX:ActiveProcessorCount=N- Override the CPU count the JVM detects (sizes GC and JIT threads, the common pool).
-XX:-UseContainerSupport- Turn OFF cgroup detection (on by default since JDK 10 / 8u191) - the JVM then sizes itself from the host.
-showversion- Print the version and continue.
Examples
$ java -XX:+PrintFlagsFinal -version | grep -E 'MaxHeapSize|UseContainerSupport|MaxRAMPercentage'What heap would a JVM get here?
$ sudo systemd-run --scope -p MemoryMax=1G java -XX:+PrintFlagsFinal -version | grep -E 'MaxHeapSize|UseSerialGC'... and inside a 1G cgroup.
Gotchas
- JAVA_TOOL_OPTIONS in the environment is read by every JVM, which prints "Picked up JAVA_TOOL_OPTIONS: ..." to stderr. It is the least invasive way to add a flag to a service.
- Below 1792MB of visible memory (or with one CPU) the JVM is not a "server class machine" and picks SerialGC unless you ask for G1.
- On linux-aarch64 the default thread stack (ThreadStackSize) is 2040K; on x86_64 it is 1024K.
Taught in
Try java in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.