Flags
-Xms sets the initial Java heap and -Xmx sets the maximum heap.
-Xlog:gc* records collector behavior so later tuning is based on real pause,
allocation, and heap-pressure data.
Flag details
- -Xms512m
- Starts the JVM with a 512 MB Java heap. A lower initial heap reduces startup footprint, but the heap may grow later and trigger additional GC work while the process warms up.
- -Xmx2g
- Caps the Java heap at 2 GB. This is not the whole process limit: metaspace, thread stacks, direct buffers, code cache, native libraries, and GC structures also consume memory.
- -Xlog:gc*:file=logs/gc.log:time,uptime,level,tags
- Enables unified GC logging for all GC tags, writes to a file, and includes enough metadata to correlate events with application logs and deployment time.
Default GC behavior
On a modern server-class HotSpot JVM, the default collector is typically G1. G1 divides the heap into regions and collects selected regions to balance throughput, memory reclamation, and pause-time goals. It performs many marking and cleanup phases concurrently, but evacuation pauses still happen when live objects are copied out of selected regions.
This recipe does not force a collector because the JVM defaults are a good first baseline. Add a collector flag only after GC logs show a real latency, footprint, or throughput problem.
When to use it
Start here for web APIs, workers, and message consumers that do not yet have a proven latency or throughput problem. On current HotSpot releases, the JVM usually selects G1 for server-class machines, so many services need only heap limits and observability first.
-Xms and -Xmx when you prefer predictable committed memory. Use a smaller -Xms when startup footprint matters.Java version support
-Xms and -Xmx are longstanding JVM options and apply across all
cataloged Java versions. -Xlog:gc* uses unified logging, so use it on Java 9+;
on Java 8 use the legacy GC logging flags instead. G1 became the default HotSpot collector
in Java 9.
Related JEPs
Examples
java -Xms512m -Xmx2g \
-Xlog:gc*:file=logs/gc.log:time,uptime,level,tags \
-jar app.jar
Use this when the service should start modestly but may grow under traffic.
java -Xms2g -Xmx2g \
-Xlog:gc*:stdout:time,uptime,level,tags \
-jar app.jar
Use equal heap bounds when you want stable committed heap and fewer resizing decisions.
Verify
Check the GC log for selected collector, pause times, heap occupancy after full cycles, and whether the process is close to -Xmx.