Friday, December 21, 2012

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 maximum heap size from 1024 to 512M it keep crashing by telling "Invalid initial heap size: -Xms=512m , Could not create the Java virtual machine" , I check for almost everything starting form 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 small case and capital case for Kilo, Mega and Gigs. you can use m or M, g or G etc but never used MB, GB or KB.  Similar problem can occur with maximum heap size specified by -Xmx. Also from Java 6 update 18 there is change on default heap size in JVM.  

Invalid initial and maximum 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:  value of either -Xms or -Xmx is higher than or close to 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:  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 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 than JVM assumes size of -Xms as 1024 bytes only and print error that its too small for JVM to start.

If you run Java program from command line than you will also get message say "Could not create the Java virtual machine" with each of invalid heap error but if you run your program from Eclipse you will not get message "Could not create the Java virtual machine", instead you will just see error message in 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 machine and on client and server JVM mode:

1) initial heap space and maximum heap space is larger for improved performance.
2) default maximum heap space is 1/2 of physical memory of size upto 192 bytes and 1/4th of physical memory for size upto 1G. So for 1G machine maximum heap size is 256MB 2.maximum heap size will not be used until program creates enough object to fill initial heap space which will be much lesser but at-least 8 MB or 1/64th part of Physical memory upto 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 its 32G for a 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