Error occurred during initialization of vm too small maximum heap

Java Programming tutorials and Interview Questions, book and course recommendations from Udemy, Pluralsight, Coursera, edX etc

Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

«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

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

In this post, we will about an error: Could not create the Java virtual machine in java.
You can provide VM arguments to provide heap size for Java virtual machine. You can specify VM argument -Xms and -Xmx in form of KB, MB or GB.
For example:
Let’s say you want to specify maximum heap size as 1024 MB then you can put VM argument as -Xmx1024M.

Memory Symbol
Kilobytes (KB) k or K
Megabytes (MB) m or M
Gigabytes (GB) g or G

There can be many reasons for this error. I will try to list down the most obvious reason for this error.
You might be getting this error because of one of below reasons.

  • Heap size is larger than your computer’s physical memory. For example,

    java -Xmx4096M MyApplication

    Error occurred during initialization of VM
    Could not reserve enough space for object heap
    Could not create the Java virtual machine.

  • You might have put space between -Xmx and 4096M. For example,

    java -Xmx 4096M MyApplication

    Invalid maximum heap size: -Xmx
    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.

  • You might have put space between -Xmx1024 and m. For example,

    java -Xmx1024 m MyApplication

    Error occurred during initialization of VM
    Too small maximum heap

    If you do not provide anything after 1024, it takes by default memory into bytes.

  • -Xms might be higher than -Xmx. For example,

    java -Xms1024M -Xmsx512M MyApplication

    Error occurred during initialization of VM
    Initial heap size set to a larger value than the maximum heap size

    Here is the solution for Initial heap size set to a larger value than the maximum heap size

  • You have put -Xms1024MB -Xmx2048MB instead of -Xms1024M -Xmx2048M. For example,

    java -Xms1024MB -Xmx2048MB MyApplication

    Invalid initial heap size: -Xms1024MB
    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.

You can check what kind of error you are getting and fix it with above solutions.
Please comment if you find some other reason for this error.

That’s all about error could not create the Java virtual machine in java.

You are here : Home / Core Java Tutorials / Series of JVM and Garbage Collection (GC) in java Best explanations ever

Contents of page :

  • 1) Read : How to write java program to pass VM/JVM parameters through CMD

  • 2) Now, we will pass VM parameters to java program to produce — Error occurred during initialization of VM Too small initial heap

  • 3) Solution of problem — Error occurred during initialization of VM Too small initial heap >

1) Read : How to write java program to pass VM/JVM parameters through CMD

How to pass vmArgs(JVM parameters) to java program in eclipse

2) Now, we will pass VM parameters to java program to produce — Error occurred during initialization of VM Too small initial heap>

Pass VM/JVM parameters -Xms

E:>java -Xms512 MyJavaProgram

Error occurred during initialization of VM

Too small initial heap

E:>

Xms is minimum heap size which is allocated at initialization of JVM in java.

Xms set is too low.

Currently Xms (minimum heap size) is set to 512 bytes.

Read :JVM Heap memory (Hotspot heap structure) with diagram in java

3) Solution of problem — Error occurred during initialization of VM Too small initial heap>

Increase Xms size.

We have set Xms (minimum heap size) is set to 512 megabytes.

This value may depend on your system configuration, so adjust accordingly.

E:>java -Xms512m MyJavaProgram

This is my Java Program

E:>

Also read : what all other VM parameters you can pass to java program through CMD or eclipse.

How to pass vmArgs(JVM parameters) to java program in eclipse

SUMMARY>

So in this core java tutorial we learned how to solve Error occurred during initialization of VM — Too small initial heap.

Having any doubt? or you liked the tutorial! Please comment in below section.

Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>

JVM (java virtual machine) in detail in java

Learn how to pass vmargs (VM parameters) to java program in eclipse?

>How Garbage Collection (GC) works internally in detail in java — BEST EXPLANATION EVER

JVM in detail — Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java

>What are Minor, Major and Full garbage collection in JVM in java

>Marking and deleting objects for garbage collection in java — Mark and sweep algorithm

Important VM parameters >

>Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java

Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java

In this short tutorial, I will be sharing how to solve the error «initial heap size set to a larger value than the maximum heap size». I have divided the post into two parts. In the first part, we will produce the error by passing VM/JVM parameters through the command line. In the second part, we will fix the error.

Read Also: Could not reserve enough space for object heap

What is -Xms and -Xmx

-Xms: In java, Xms is the minimum heap size that is allocated at initialization of JVM.
-Xmx: Xmx represents the maximum heap size JVM can use.

1. Producing the Error

Suppose we have below HelloWorld java program:

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

Compile the above HelloWorld java program in command line/terminal:

javac HelloWorld.java

1. Run the above program by passing a single JVM parameter:

Passing -Xms

java -Xms512m HelloWorld

The above command will print the following output:

Simple Java Program

2. Run the HelloWorld program by passing multiple JVM parameters:

Passing -Xms and -Xmx  

java -Xms512m -Xmx1024m HelloWorld

After running the above command, you will get the following output:

Simple Java Program

If the value of Xms(minimum heap size) is greater than the value of Xmx(maximum heap size) than it will produce this error as shown below:

java -Xms2g -Xmx1g HelloWorld

Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size

Currently, the value of Xms(minimum heap size) is 2 gigabytes and Xmx(maximum heap size) is 1 gigabyte. 

2. [Fixed] Initial heap size set to a larger value than the maximum heap size

To get rid of this error, the value of Xmx(maximum heap size) should always be greater than or equal to Xms(minimum heap size).

Run the HelloWorld program with the value of Xms(minimum heap size) set to 1 gigabyte and Xmx(maximum heap size) set to 2 gigabytes.

java -Xms1g -Xmx2g HelloWorld

Simple Java Program

That’s all for today, please mention in comments in case you have any questions related to the initial heap size set to a larger value than the maximum heap size error.

Понравилась статья? Поделить с друзьями:
  • Error occurred during initialization of vm intellij idea
  • Error occurred during initialization of vm initial heap size
  • Error occurred during initialization of vm gradle
  • Error occurred during initialization of vm failed setting boot class path
  • Error occurred during initialization of vm could not reserve enough space for 4145152kb object heap