Skip to main content
JVM tuning and diagnostics · Topic 3 of 5

Heap sizing flags

Definition​

The heap is the memory for objects. -Xms sets its starting size and -Xmx its maximum. In containers, -XX:MaxRAMPercentage sets the maximum as a share of the container memory. Leave room for memory outside the heap, such as thread stacks and metaspace.

Example code​

Sizing the heap and getting a dump if it fills up

# fixed numbers
java -Xms512m -Xmx2g -jar app.jar

# in a container: a share of the memory limit (leave the rest for stacks, metaspace and native memory)
java -XX:MaxRAMPercentage=70 -jar app.jar

# on OutOfMemoryError: write a heap dump to analyse, then stop so the platform restarts the app
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.hprof -XX:+ExitOnOutOfMemoryError -jar app.jar

Finished this page? Track your progress (saved only in your browser).