[Solved] Initial heap size set to a larger value than the maximum heap size

In this short tutorial, I will be sharing how to solve the error "initial heap size set to a larger value than the maximum heap size". I have divided the post into two parts. In the first part, we will produce the error by passing VM/JVM parameters through the command line. In the second part, we will fix the error.

Read Also: Could not reserve enough space for object heap

What is -Xms and -Xmx

-Xms: In java, Xms is the minimum heap size that is allocated at initialization of JVM.
-Xmx: Xmx represents the maximum heap size JVM can use.

1. Producing the Error

Suppose we have below HelloWorld java program:

  public class HelloWorld
       public static void main( String args[] ) {
System.out.println("Simple Java Program");
}
}

Compile the above HelloWorld java program in command line/terminal:

javac HelloWorld.java

1. Run the above program by passing a single JVM parameter:

Passing -Xms

java -Xms512m HelloWorld

The above command will print the following output:

Simple Java Program

2. Run the HelloWorld program by passing multiple JVM parameters:

Passing -Xms and -Xmx  

java -Xms512m -Xmx1024m HelloWorld

After running the above command, you will get the following output:

Simple Java Program

If the value of Xms(minimum heap size) is greater than the value of Xmx(maximum heap size) than it will produce this error as shown below:

java -Xms2g -Xmx1g HelloWorld

Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size

Currently, the value of Xms(minimum heap size) is 2 gigabytes and Xmx(maximum heap size) is 1 gigabyte. 

2. [Fixed] Initial heap size set to a larger value than the maximum heap size

To get rid of this error, the value of Xmx(maximum heap size) should always be greater than or equal to Xms(minimum heap size).

Run the HelloWorld program with the value of Xms(minimum heap size) set to 1 gigabyte and Xmx(maximum heap size) set to 2 gigabytes.

java -Xms1g -Xmx2g HelloWorld

Simple Java Program


That's all for today, please mention in comments in case you have any questions related to the initial heap size set to a larger value than the maximum heap size error.

0 Comments