← Recipes

Memory-Constrained Service

Reduce committed heap slack when process footprint is more important than peak throughput.

Flags

-XX:MaxHeapFreeRatio-XX:MinHeapFreeRatio-Xmx

-XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio influence how much free heap the JVM tries to keep after collection. Lower values can reduce footprint at the cost of more heap resizing or GC activity.

Flag details

-Xmx512m
Limits the Java heap to 512 MB. This protects the host or container from unbounded heap growth but does not limit total process RSS.
-XX:MaxHeapFreeRatio=20
Asks the JVM to shrink the heap when too much of it is free after GC. Lower values can return memory sooner.
-XX:MinHeapFreeRatio=10
Asks the JVM to keep at least this percentage of heap free after GC. Too low a value can cause more frequent heap expansion and collection.
-Xlog:gc*
Shows whether shrinking and growing the heap creates extra GC activity or latency.

GC behavior

These flags do not select a collector. They influence heap sizing decisions made around GC cycles. Lower free-ratio targets can reduce committed heap after load drops, but they can also make the JVM resize the heap more often when traffic is bursty.

When to use it

Use this for small services, sidecars, developer tools, or shared hosts where RSS matters and latency or throughput targets are modest.

Java version support

-Xmx, -XX:MinHeapFreeRatio, and -XX:MaxHeapFreeRatio are longstanding HotSpot options and apply across all cataloged Java versions. The logging form in the examples, -Xlog:gc*, applies to Java 9+.

Related JEPs

Examples

java -Xmx512m -XX:MaxHeapFreeRatio=20 -XX:MinHeapFreeRatio=10 \
  -Xlog:gc*:file=logs/footprint-gc.log:time,uptime,level,tags \
  -jar app.jar

Use this only after comparing process RSS and GC activity against the default free-ratio behavior.

Verify

Compare RSS, GC frequency, and response latency before and after lowering the free-ratio values.