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:
The given command indicates that the heap starts from “4” MB till the maximum of “64” MB.
The above size signifies that the heap starts from “50” MB till the default maximum.
Likewise, here the heap initiates from the default initial value till a maximum of “256” MB.
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.