Java Xmx

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.

Java Xmx

Step 2: Click on the Advanced system settings. It opens the System Properties window.

Java Xmx

Step 3: In the System Properties window, click on the Environment Variables. It opens the Environment Variables dialog box.

Java Xmx

Step 4: In the Environment Variables dialog box, click on the New button presented under the System Variables.

Java Xmx

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.

Java Xmx

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.

Java Xmx

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.

Java Xmx

In the above result, there are four command-line arguments related to Java application memory.

  • -Xnoclassgc: Disable class garbage collection.
  • -Xms<size>: The command sets the initial size of the Java heap. Its default size is 2M (2097152 bytes). The size should be greater than 1024 byte (1KB). It must be the multiple of 1024.
  • -Xmx<size>: Set maximum Java heap size.
  • -Xss<size>: Set Java thread stack size.

Examples

Let's see some examples based on Xmx.

  • -Xms2m -Xmx80m: Heap starts growing from 2MB to 80MB.
  • -Xms70m -Xmx70m: Heap starts at 70 MB and it will never grow.
  • -Xms40m: Heap starts at 40 MB and grows to the default maximum size.
  • -Xmx256m: Heap starts at default initial value and grows to a maximum of 256 MB.