JVM heap options: Xmx and Xms

2 min read | by Jordi Prats

The Xmx and Xms settings are most commonly overlooked settings but yet quite critical for the JVM to perform as expected. They control the JVM's heap: the memory area where objects are instantiated.

The settings that we can use for tunning the JVM are:

  • Xmx (minimum heap size) flag specifies the maximum amount of heap that can be allocated
  • Xms (maximum heap size) specifies the initial memory allocation pool

With these settings the JVM will try to keep as close as possible to the Xms and grow up to Xmx. We'll have to keep in mind that:

  • Setting Xms too low it will cause the JVM to trigger the garbage collector more often, causing bad performance
  • Setting Xmx too low can cause out of memory exceptions (java.lang.OutOfMemoryError: Java Heap Space)
  • Setting either Xms or Xmx too high can waste a lot of memory

Oracle recommends setting the -Xms equal to the -Xmx for minimizing garbage collections, but it's not always the best setting. The best way of adjusting these settings it to keep an eye on the JVM statistics (JConsole is a good start) to be able to adjust these settings to the needs of your application.

Keep in mind that these settings are just for the JVM's heap. The JVM is going to use more memory for other areas than just the size allocated to the heap


Posted on 15/04/2021

Categories