In this section, we will understand what is Xmx in Java, how to set up maximum heap size for the Java application.
In Java, sometimes when we run the Java application it shows an error message like the following:
Error occurred during initialization of VM. Could not reserve enough space for object heap. Could not create the Java virtual machine.
The above error indicates that increases the heap size for running the application. There is no sufficient space for creating the object. The solution to this error is to increase the heap size by using the Java -Xmx argument along with the specified heap size. Let's see what is -Xmx in Java.
What is Xmx in Java?
In Java, Xmx is a flag that specifies the maximum memory (in bytes) allocation pool for the JVM. It sets up the maximum heap size. Remember that the value must be in multiples of 1024 and must be greater than 2MB. It uses the letter K or k to represents the size in kilobytes, and the letter M and m to represent the size in megabytes. The default heap size is 64M.
Default Heap Size
The default initial maximum heap size is based on the ergonomics algorithm. In Java, the initial heap size is 256M or 266634176 bytes and the maximum heap size is 4068M or 4266146816 bytes.
We recommend you to set the maximum heap size between 512M to 1024M for small and medium Java application.
Setting up Maximum Heap Size
We can increase the heap size to any value that we want but remember that RAM must support that value. Suppose, we want to set the maximum heap size to 80M. We can use any of the following flags for setting up the maximum heap size. All the commands allocate the same heap size.
The above Xmx flags allocate the Java heap memory to a maximum size of 80MB. Note that there is no space between the flag (-Xmx) and memory size (80M). It does not matter if you are using an uppercase or lower-case letter. For example, -Xmx80M is the same as -Xmx80m. It means that the application can use the maximum heap size of 80M of the available memory. Therefore, the -Xmx flag in Java, changes the maximum heap size for the JVM.
Follow the steps given below for setting up the maximum heap size.
Step 1: Open the Control Panel and click on the System icon.
Step 2: Click on the Advanced system settings. It opens the System Properties window.
Step 3: In the System Properties window, click on the Environment Variables. It opens the Environment Variables dialog box.
Step 4: In the Environment Variables dialog box, click on the New button presented under the System Variables.
Step 5: In the window, provide the Variable name _JAVA_OPTIONS and Variable value -Xmx512m. You can put any value in place of 512. After that, click on the OK button three times.
Now, we have set the maximum heap size 512M.
If you are using Eclipse IDE, the same this can be done by specifying the VM arguments, as we have shown below.
Why do we need to maximize the heap size?
If the Java process has exceeded the -Xmx maximum Java heap size, the popular java.lang.OutOfMemoryError will be thrown.
Other Command-line Arguments Related to Memory
Java provides more options for controlling the Java application memory. If we want to see which options are available for memory, we can use the java -X command. It shows the different options available for handling the memory.
In the above result, there are four command-line arguments related to Java application memory.
Let's see some examples based on Xmx.