Sunday, 26 January 2014

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

No comments:

Post a Comment