JVM
Java Virtual Machine
JVM parameters
-Xms
-Xmx

What are the -Xms and -Xmx JVM parameters in Java?

Codemia Knowledge Hub ·

-Xms sets the initial heap size of the Java Virtual Machine, and -Xmx sets the maximum heap size it is allowed to use. Together they control how much memory the JVM takes at startup and how far the heap can grow, which affects performance and keeps the application from consuming more memory than the system has.

-Xms: initial heap size

-Xms is the amount of memory the JVM tries to allocate at startup. It influences performance in the early phase of execution: if it is too low, the JVM may run frequent garbage collections (GC) before the heap settles at a suitable size, slowing startup.

 
java -Xms512m -jar application.jar

This starts the JVM with an initial heap of 512 megabytes.

-Xmx: maximum heap size

-Xmx caps the heap. If the application tries to allocate beyond it, the JVM throws an OutOfMemoryError. Too high a value can waste system resources and affect other applications; too low a value causes frequent garbage collections and possible OutOfMemoryError when the heap cannot hold the objects the application creates.

 
java -Xmx1024m -jar application.jar

This sets the maximum heap size to 1024 megabytes.

Why tuning the heap matters

  1. Performance: The JVM has enough memory from the start and does not keep asking the operating system for more.
  2. Resource management: Capping Java's memory use protects the host operating system and other applications.
  3. Stability: With adequate memory from the start, the memory footprint grows and stabilizes earlier, so behavior under load is more predictable.

Choosing values for -Xms and -Xmx

  • Match -Xms and -Xmx: Setting both to the same value removes the need for the JVM to resize its heap, which makes behavior more predictable.
  • Consider the environment: Account for the application's needs, total system memory, and other applications on the same machine.
  • Monitor and adjust: Use jstat, jconsole, or commercial monitoring tools to watch memory consumption and tune the values.
ParameterPurposeTypical Setting
-XmsSets the initial JVM heap size-Xms256m or higher
-XmxSets the maximum JVM heap size-Xmx2048m or based on app requirements

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms