Java -Xmx is the configuration parameter to control the amount of memory Java uses on a system. Basically these settings are there to control the Heap memory size of Java. There are two settings related to Java heap memory:
- -Xmx to set the maximum heap memory size
- -Xms to set the minimum heap memory size
Tips to set the Java heap memory size
Managing the Java heap memory size for a server is very crucial as the whole performance depends on this memory size, off course there are other factors which affects the performance. So let’s see how you can set these parameters to control the Java heap memory size.
Do not set -Xmx to too small value
If you set -Xmx too small for your server then your application may not work properly and you may get Out of memory exception. So never set this too small as this is the maximum amount of memory you are allocating for Java and it cannot utilize memory beyond the set value.
It is always advisable to set -Xmx to a higher value if you have enough memory space available on your server. On the other hand if you set -Xmx value to a higher value your other resources will not be able to perform well as you have already reserved some of your memory for Java. So before setting the maximum heap size memory just check how much memory is free. To do so, stop your application server and check the free memory and accordingly you can set the maximum memory size. For example if you have 512M free memory then you can set heap memory to 300M safely i.e. -Xmx300m.
Set -Xms to a small value
If you set -Xms to higher value you might run out of memory. So always try to keep it to a small value like -Xms16m. In tomcat when you restart the server it starts a Java process and that process takes the same -Xms as the default value for the tomcat and if it is higher you may get out of memory.
Now let’s see how to set java heap memory in different servers.
How to set java heap size in Tomcat
To set the java heap size in tomcat you need to edit the catalina.sh (On Linux) or catalina.bat (On Windows) file. You can find these files inside the bin directory of tomcat. Open the appropriate file and search for CATALINA_OPTS and set the value as shown below, you can change the max and min value to whatever you want.
set CATALINA_OPTS=-Xms64m -Xmx512m
export CATALINA_OPTS=”-Xms64m -Xmx512m”
setenv CATALINA_OPTS “-Xms64m -Xmx512m”
You can download this PDF file to check how to set java heap memory size for other servers.