Java

Xms and Xmx Java

While creating application(s), there can be a requirement where the programmer needs to enhance the performance and memory. In such case scenarios, the “Xms” and “Xmx” parameters play a vital role in manipulating the heap size utilized by Java Virtual Machine. These parameters can be stated in various formats like KB, MB, etc.

This write-up will illustrate the usage of the “Xms” and “Xmx” parameters in Java.

What are “Xms” and “Xmx” in Java?

Xms” and “Xmx” in Java refer to the parameters that are utilized to adjust the heap size. It works in such a way that the former parameter allocates the minimum heap size and the latter parameter assigns the maximum heap size.

-Xms

It is utilized to set the initial heap size. Allocating the minimum heap size identical to the maximum heap size is advised to refrain from garbage collection.

-Xmx

It is used to assign the maximal heap size. The performance is affected if the maximum heap value is allocated lower compared to the amount of live data.

Syntax

Syntax Usage Default
-Xms<size> Allocates the minimum heap size. 8 MB
-Xmx<size> Allocates the maximal heap size. 25% of available/free memory

Important Considerations

The following points should be considered before working with the discussed parameters:

  • If “-Xms” is allocated greater than “-Xmx”, the VM fails and will log the following message “-Xms too large for -Xmx”.
  • In the other case, if the limit set by the “-Xmx” parameter is exceeded, the VM gives an “OutofMemory” limitation.

Examples

The following examples cover all the possible outcomes of the discussed parameters:

-Xms4m -Xmx64m

The given command indicates that the heap starts from “4” MB till the maximum of “64” MB.

-Xms50m

The above size signifies that the heap starts from “50” MB till the default maximum.

-Xmx256m

Likewise, here the heap initiates from the default initial value till a maximum of “256” MB.

-Xms50m -Xmx50m

The above sizes state that the heap initiates at “50” MB and remains intact, thereby never growing.

Conclusion

The “-Xms” and “-Xmx” parameters in Java assign the minimum and the maximum heap sizes, respectively and the heap can never grow larger than “-Xmx”. Also, these parameters are recognized by the Java virtual machine (JVM). This blog discussed the functionalities of the “-Xms” and “-Xmx” parameters in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.