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.
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.
This sets the maximum heap size to 1024 megabytes.
Why tuning the heap matters
- Performance: The JVM has enough memory from the start and does not keep asking the operating system for more.
- Resource management: Capping Java's memory use protects the host operating system and other applications.
- 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
-Xmsand-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.
| Parameter | Purpose | Typical Setting |
-Xms | Sets the initial JVM heap size | -Xms256m or higher |
-Xmx | Sets the maximum JVM heap size | -Xmx2048m or based on app requirements |
Related reading
- What are the advantages and disadvantages of using feign over RestTemplate
- What are the differences between ArrayList and Vector?
- What are the differences between generic types in C and Java?
- What are the differences between Generics in C and Java... and Templates in C?
- What are the differences between gradle assemble and gradle build tasks?
- What are the differences between the different saving methods in Hibernate?
- What are the effects of exceptions on performance in Java?
- What are the main uses of yield, and how does it differ from join and interrupt?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.