Friday, July 30, 2021

Invalid initial and maximum heap size in JVM - How to Fix

I was getting "Invalid initial heap size: -Xms=1024M" while starting JVM and even after changing the maximum heap size from 1024 to 512M it keeps crashing by telling "Invalid initial heap size: -Xms=512m, Could not create the Java virtual machine". I check almost everything starting from checking how much physical memory my machine has to any typo in JVM parameters, only to find out that instead of M, I had put MB there. Java accepts both the small case and the capital case for Kilo, Mega, and Gigs. you can use m or M, g or G, etc but never used MB, GB, or KB.  A similar problem can occur with the maximum heap size specified by -Xmx. Also from Java 6 update 18, there is a change in default heap size in JVM.  


Invalid initial and maximum heap size in JVM

Here is a list of common errors while specifying maximum and minimum heap size in Java :


java -Xmx4056M -Xms4056M HelloWorld
Issue:  Error occurred during initialization of VM , The size of the object heap + VM data exceeds the maximum representable size



Cause:  the value of either -Xms or -Xmx is higher than or close to the size of physical memory, as my machine has 4GB memory.





java -Xmx1056M -Xms2056M HelloWorld
Issue:  Error occurred during initialization of VM , Incompatible minimum, and maximum heap sizes specified

Cause: the value of -Xms is higher than -Xmx



java -Xms2056M HelloWorld
Issue: Error occurred during initialization of VM , Could not reserve enough space for object heap

Cause: Only -Xms was provided and -Xmx was not provided. you will also get this error if you have a typo and instead of -Xmx you have specified -Xms two times, happened to my friend last time.



Command: java -Xms1024 M -Xmx1024M HelloWorld
Issue: Error occurred during initialization of VM , Too small initial heap

Cause: If you had space between 1024 and M then JVM assumes the size of -Xms as 1024 bytes only and prints an error that it's too small for JVM to start.


Invalid heap size

Invalid initial and maximum heap size in JVMAnother scenario when the "invalid heap size" issue comes while restarting JVM is when you configure 64 bit JVM to accept the memory of more than 4GB but it's running on a 32-bit data model. This "invalid heap size" occurs particularly in the Solaris box where J2SE installation contains both 32 bit and 64-bit J2SE implementation.

On other environments like Windows and Linux, 32 bit and 64 bit JVM are installed separately. 64 bit JVM installed on Solaris machines runs with a 32-bit model if you don't specify either -d32 or -d64, which won't accept a Maximum heap size of 4GB, hence "invalid heap size".

You can resolve this issue by running Solaris JVM with option -d64.  -d64 command-line option allows JVM to use a 64-bit data model if available.

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

$ test@nykdev32:~ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode)

$ test@nykdev32:~ java -Xmx4096M HelloWorld
Invalid maximum heap size: -Xmx4096M
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.

$ test@nykdev32:~ java -d64 -Xmx4096M HelloWorld
HelloWorld to Java

If you run the Java program from the command line then you will also get a message that say Could not create the Java virtual machine with each invalid heap error but if you run your program from Eclipse you will not get the message "Could not create the Java virtual machine", instead, you will just see an error message in the console.

Regarding default heap size in Java, from Java 6 update 18 there are significant changes in how JVM calculates default heap size in 32 and 64-bit machines and on client and server JVM mode:

1) Initial heap space and maximum heap space is larger for improved performance.


2) The default maximum heap space is 1/2 of the physical memory of size up to 192 bytes and 1/4th of physical memory for a size up to 1G. So for a 1G machine maximum heap size is 256MB 2.maximum heap size will not be used until the program creates enough objects to fill the initial heap space which will be much lesser but at least 8 MB or 1/64th part of Physical memory up to 1G.


3) For Server Java virtual machine default maximum heap space is 1G for 4GB of physical memory on a 32 bit JVM. for 64 bit JVM it's 32G for physical memory of 128GB.


Other Java tutorials you may find useful

4 comments :

Anonymous said...

Hello, I am getting following error, while starting a core Java based Server in a shared Solaris host "Error occurred during initialization of VM
Could not reserve enough space for object heap" , Program was running fine few days ago, but when I am restarting it now, it's giving me this error. There is no change in JVM arguments passed to program, I also tried with different heap size (4GB, 2GB, 500MB) , but still same error, please help

Anonymous said...

Thanks for this, this saved my day. I am getting below error in Maven, while running mvn install while building a Java project on Eclipse IDE :

Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Later I realized that I was using JDK 1.7 32-bit virtual machine and configuring Maven's surefire plugin, which is used to run all JUnit test cases with following JVM memory arguments :

-Xms800m -Xmx800m -XX:MaxPermSize=500m

Since this configuration was actually for 64-bit JVM, it was throwing Invalid Heap Size error. There was two ways to fix that, either moving to 64-bit JDK or reducing memory size to half e.g.

-Xms400m -Xmx400m -XX:MaxPermSize=200m

For me second solution works fine. Thanks

Anonymous said...

I am getting following error while running my maven build :

Error occurred during initialization of VM
Incompatible minimum and maximum heap sizes specified

Please help

Anonymous said...

The particular error listed at the top ("Invalid initial heap size: -Xms=1024M") is caused by having the equals sign, so changing it to just "-Xms1024M" should take care of it, as I just found out from a misconfiguration of my own.

Post a Comment