Friday, October 19, 2012

Could not create the Java virtual machine Invalid maximum heap size: -Xmx

"Could not create the Java virtual machine" is a general Java Virtual Machine error when you run java command directly or indirectly and its not able to create virtual machine because of invalid maximum heap size, invalid minimum heap size or just any error in command line. This error not only come when you run Java program from command line but also when you run them using any IDE like Eclipse or Netbeans. Could not create the Java virtual machine invalid maximum heap size -Xmx is a special case for it which comes when you try to run your java command with -Xmx option and value of that option is invalid like syntax error, value error or anything else. In this Java article we will see couple of scenarios when you get "Could not create the Java virtual machine Invalid maximum heap size: -Xmx" which will help you to avoid those error and fix this problem.

1) You run Java command with -Xmx and heap size is more than what java can have on that operating system.
Could not create the Java virtual machine Invalid maximum heap sizeAs we discussed in 10 points of Java heap space that maximum heap size varies based upon machine architecture e.g. 32 bit or 64 bit, JVM bit size e.g. 32 bit JVM or 64 bit JVM and operating system. In 32 bit machine though theoretical limit of maximum heap size is 4GB, it varies on operating system to operating system e.g. on 32 bit windows XP maximum heap size limits upto 1.5G due to various reason while on 64 bit Solaris machine even with 32 bit JVM you can afford around 3.5GB. So when  you run following java command in 32 bit Windows XP machine, you will get "Could not create the Java virtual machine : Could not reserve enough space for object heap" and Error occurred during initialization of VM

~/java java -Xmx1800M Hello
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine

2) "Could not create the Java virtual machine : Invalid maximum heap size -Xmx" error actually comes due to syntactical error. for example look at the below java command and see if you can spot the reason of "Could not create the Java virtual machine : Invalid maximum heap size -Xmx"

~/java java -Xmx 1400M Hello
Could not create the Java virtual machine.
Invalid maximum heap size: -Xmx

If you look carefully you will space between -Xmx and 1800M, which is causing "Could not create the Java virtual machine : Invalid maximum heap size -Xmx". syntactically there should not be any space between -Xmx and heap size. Just remove the space between -Xmx and 1400M and it will work.

~/java java -Xmx1400M Hello
Hi..... Java

Another common syntax error while specifying maximum heap size is using world lik KB, MB or GB.
~/java java -Xmx1500MB
Could not create the Java virtual machine.
Invalid maximum heap size: -Xmx1500MB

Java only allows k or K, m or M and g or G after specifying size of heap in numbers e.g. -Xmx1400M is valid but -Xmx1400MB is invalid heap size.

One more worth noting syntax error while providing heap space is space between numeric literal and unit, as shown in below example:

~/java java -Xmx1500 M
Error occurred during initialization of VM
Too small initial heap

This would not result in "Could not create the Java virtual machine : Invalid maximum heap size -Xmx" but still you won't be able to start your java program because its only considering maximum heap size as 1500 Bytes which is ways lower than minimum default heap size. to avoid this do not use space between number and unit e.g. 1500 M is invalid, while 1500M is valid.

These were some of the common mistakes made by Java programmer while specifying maximum heap size. Which eventually result in "Could not create the Java virtual machine : Invalid maximum heap size -Xmx" and stops Java program or application from starting.
Other Java Tutorials You may like

1 comment:

  1. We have couple of solutions for the above Problem

    Solution 1: You can re-install the all components. ie it means you have install the entire s/w. for the Error: Could not create the Java Virtual Machine.

    Solution 2: that maximum heap size varies based upon machine architecture e.g. 32 bit or 64 bit, JVM bit size e.g. 32 bit JVM or 64 bit JVM and operating system.

    In 32 bit machine though theoretical limit of maximum heap size is 4GB, it varies on operating system to operating system e.g. on 32 bit windows XP maximum heap size limits upto 1.5G due to various reason while on 64 bit Solaris machine even with 32 bit JVM you can afford around 3.5GB. So when you run following java command in 32 bit Windows XP machine

    Solution 3: One more worth noting syntax error while providing heap space is space between numeric literal and unit, as shown in below example: Correct: ~/java java -Xmx1500 M In-correct: ~/java java -Xmx1500MB In-Correct: ~/java java -Xmx 1400M

    Regards, Ravindra Seelam

    ReplyDelete

Java67 Headline Animator