Sunday, July 11, 2021

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

"Could not create the Java virtual machine" is a general JavaVirtual Machine error when you run java command directly or indirectly and it's not able to create a virtual machine because of invalid maximum heap size, invalid minimum heap size, or just an error in command line. This error not only come when you run Java program from the 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 a 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 errors and fix this problem.

Btw, If you are serious about mastering JVM and Java performance in-depth then you can also check out Java Application Performance and Memory Management course by Matt Greencroft. It's a great course for experienced Java developers.





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


1) You run Java command with -Xmx and heap size is more than what java can have on that operating system.

As 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 like 32 bit JVM or 64 bit JVM and operating system. 

In 32 bit machine through the theoretical limit of maximum heap size is 4GB, it varies on the operating system to the operating system like on 32-bit Windows XP maximum heap size limits up to 1.5G due to various reasons while on 64 bit Solaris machine even with 32 bit JVM you can afford around 3.5GB. 

So when  you run the 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

You can also see these JVM-related courses to learn more about JVM concepts like heap memory in Java.

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


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 words like KB, MB, or GB.

~/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 the size of the heap in numbers like -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 the unit, as shown in the 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 the 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 programs or applications from starting.


Other Java Tutorials You may like


2 comments:

  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
  2. Can u plz post a coding to calculate rate of interest in java

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.