Error occurred during initialization of vm too small initial heap

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

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

Thread Status:

Not open for further replies.
  1. I was recently playing on my server with 6-8 people. I have 1gb of Ram for my server. Then while I was working on a project, my server crashed. I do not have a crash report for this unfortunately. Now whenever I try to launch it, I get the error of

    «Error occured during initialization of VM
    Too small initial heap
    Press any key to continue»

    Is it a RAM issue? If so, how can I fix this?
    My note for the batch file is

    @ECHO OFF
    SET BINDIR=%~dp0
    CD /D «%BINDIR%»
    «%ProgramFiles(x86)%Javajre7binjava.exe» -Xmx1024M -Xms1024 -jar craftbukkit.jar
    PAUSE

  2. Curiously, have you restarted your computer since it crashed?

  3. Yes I have restarted my computer but still nothing

  4. see if changing the amount of ram helps… to do so change this: -Xmx1024M -Xms1024
    to this: -Xmx2024M -Xms2024M
    That will give it 2gb instead of 1gb

  5. Okay So I changed it, tried to run the server, and got this error —
    «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.
    Press any key to continue . . .»

  6. You didn’t specify «M» for megabytes in your initial script on the -Xms parameter

    Also, if setting it to 2024 (ahem, correct amount for 2GB is 2048) causes the «Could not reserve enough space for object heap» error, that means your computer doesn’t have that much RAM free. If you are telling it you MUST have 2GB memory to start the program, it will tell you «sorry, that can’t happen, bro» and fail to start.

    FYI:
    -Xmx is your *maximum heap size*; the server will never be allowed to use more than this amount of RAM
    -Xms is your *initial heap size*; when the program starts it will be given this much memory (which can grow if needed up to the -Xmx limit). If there is not at least this much memory free when the program starts, it will fail (like the second error you got). Without the «M» to specify that 1024 was megabytes, you gave the server a starting allocation of 1kb which is too small (which is why you got the first error).

    Try this:

    @ECHO OFF
    SET BINDIR=%~dp0
    CD /D «%BINDIR%»
    «%ProgramFiles(x86)%Javajre7binjava.exe» -Xmx1G -Xms1G -jar craftbukkit.jar
    PAUSE

    That will give you 1GB (the same as 1024M) heap to start with, and it will never use more than that 1GB.


    Starrz and mmorton89 like this.

  7. lol >_> I can’t believe I didn’t see that last time I saw this. 4am is correct lol.

  8. oh wow lol i see it to now… fail

  9. THANK YOU SOO MUCH FOR THE EXPLANATION AND FOR THE NEW NOTE FOR THE BATCH!!! IT WORKS PERFECTLY FINE NOW!!!:D

  10. The trick with server administration is learn as much as you can about how everything works — one of the reasons Minecraft is such a great game is that it has inspired so many people (especially younger people) to figure out stuff like this. You now know how to pass memory-usage parameters to Java progams from the command line! Skillz, yo. Keep learning everything you can about how it all works!

    EDIT: Oh yeah, glad I could help :)

Thread Status:

Not open for further replies.

Share This Page


Bukkit Forums

In this post, we will see an error(Could not reserve enough space for 2097152kb object heap object heap) which you might have encountered while dealing with JVM.We will see how can we fix this issue.

Table of Contents

  • Heap size
    • Maximum heap size
  • Cause 1: Did not specify heap size
    • Fix 1
  • Cause 2: Too large Xmx value
    • Fix 2
  • Cause 3: Specifying large heap size more than physical memory
    • Fix 3
    • Set _JAVA_OPTIONS environment variable
  • Could not reserve enough space for 2097152kb object heap
    • Apache cordova
    • Minecraft
    • Jfrog artifactory
  • Conclusion

error occurred during initialization of vm could not reserve enough space for 2097152kb object heap is generally raised when Java process can not create java virtual machine due to memory limitations.

Before we go through causes and fixes for this issue, let’s go through few basic things.

Heap size

Heap size is memory allocation space for storing java objects at run time. This heap size can have minimum and maxiumn heap size. You can specify minimum and maximum size using Xmx and Xms VM arguments.

Maximum heap size

The maximum possible heap size can be determined by available memory space. It is different on 32 bit and 64 bit as follows.

  1. 2^32 (~4GB) on 32 bit JVM
  2. 2^64 (~16EB) on 64 bit JVM.

In general, you will get 1.4-1.6 GB on 32 bit windows and approximately 3GB on on 32 bit linux.

If you require large heap, then you should generally use 64 bit JVM.

Cause 1: Did not specify heap size

Let’s say you run your java program without specifying any heap size and you get below error.

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

You will get this error more often in 32 bit JVM rather than 64-bit JVM

Reason
32-bit Java requires contiguous free space in memory to run. If you specify a large heap size, there may not be so much contiguous free space in memory even if you have much more free space available than necessary.
Installing 64-bit version might solve this issue in this case.

Fix 1

You can fix this error by running Java with lower heap size such as -Xmx512m.

java Xmx512M MyApplication

Cause 2: Too large Xmx value

If you specify too large memory with -Xmx option on 32 bit VM, you may also get this error.
For example:
Let’s say you are getting an error with below execution.

java Xms1536M Xmx1536M MyApplication

Fix 2

You might not have enough contiguous free space in memory.You can run the application with slightly lower heap size to resolve the issue.

javaXms1336M Xmx1336M MyApplication

Cause 3: Specifying large heap size more than physical memory

If you specify large heap size more than physical memory available on 64-bit or 32-bit  machine, you will get this error.
For example:
Let’s say You have 3 GB RAM on your machine and you are executing below command, you will get this error.

java Xms4096M Xmx4096M MyApplication

Fix 3

You can run the application with heap size which is less than your physical memory.

javaXms2048M Xmx2048M MyApplication

Sometimes above solutions might not work.

Set _JAVA_OPTIONS environment variable

So you can set _JAVA_OPTIONS as the environment variable.

In Linux

-bash-3.2$ export _JAVA_OPTIONS =»-Xmx512M»
-bash-3.2$ javac MyApp.java

In Window

Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System
Variables->New: Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M

💡 Did you know?

JDK_JAVA_OPTIONS is prefered environment variable from Java 9+ onward to specify Java options. It will be ignored in the version lesser than Java 9.

You might get an specific error Could not reserve enough space for 2097152kb object heap in case you are using any tool. It simply means that JVM is not able to acquire 2 GB heap space which is required by the tool by default.

Apache cordova

Apache Cordova is a mobile application development framework originally created by Nitobi.
If you are getting this error on Apache cordova, here are solutions.

  1. Switch from 32 bit JVM to 64 bit JVM
  2. set _JAVA_OPTIONS environment variable with -Xmx512M
  3. Change

    args.push(‘-Dorg.gradle.jvmargs=-Xmx2048m’)

    to  

    args.push(‘-Dorg.gradle.jvmargs=-Xmx1024m’);

    on the following files located on your machine.

    project-folderplatformsandroidcordovalibbuildersbuilders.js
    project-folderplatformsandroidcordovalibbuildersGradleBuilder.js
    project-folderplatformsandroidcordovalibbuildersStudioBuilder.js

Minecraft

If you are getting this error, while launching Minecraft game, then you need to switch from 32 bit JVM to 64 bit JVM.

Jfrog artifactory

If you are using JFrom artifactory as artifact life-cycle management tool and getting Could not reserve enough space for 2097152kb object heap while launching it.
Go to bin directory of your JFrog Artifactory installation, change following line in arifactory.bat file.

set JAVA_OPTIONS=-server Xms512m Xmx2g Xss256k XX:+UseG1GC

to

set JAVA_OPTIONS=-server Xms512m Xmx1024m Xss256k XX:+UseG1GC

This should resolve this error for JFrog Artifactory installation.

Conclusion

  • If you might have installed 32 bit JVM in 64 bit machine.Upon uninstalling that version and installing 64 bit version of Java
  • You might have provided too large heap size which might be greater than physical memory. In this case, you need to reduce heap size of JVM.
  • If above solution does not work, you can set JAVA_OPTS as the environment variable to solve this issue. If you set JAVA_OPTS environment variable, then each JVM initialization will automatically use configuration from this environment variable

I hope this will resolve your issue with error occurred during initialization of vm could not reserve enough space for 2097152kb object heap.

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

@pio2398

Startup bat script don’t like 32 bit Java on 64 bit system. When i try to start i got this:
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.
I can fix this problem by removing «-Xmx2048m» from DEFAULT_JVM_OPTS but i don’t not know if this has side effects.
Commit 8d90769 probably have also a bug. Install script should check first for «native» Java and later 32-bit Java on 64 bit system(Wow6432Node). If this will be helpful i can’t come on IRC tomorrow but i am not a expert.

@pio2398

Probably best solution on 64 bit OS is giving hint to install 64 bit Java and use it. But this issue likely also applies 32 bit system.

@binwiederhier

(1) Commit dd3f3ae changes the order of registry keys to check, so the native Java versions should be checked first.

(2) The -Xmx2048m «problem» is a different one: «-Xmx» tells Java to expect a max. memory usage of 2 GB. Your machine complaining with «Error occurred during initialization of VM» means that you don’t have that much physical memory installed (or available?) For small repositories 2 GB are not necessary, but we had issues with extremely large repositories (200k+ files, see issue #115). Are you running Syncany in a Virtual Machine (like with VirtualBox)?

Also: I am not sure if we can make this work for everyone — large repositories and small machines. We’d have to come up with something clever …

@pio2398

I have free almost 6 GB of RAM. I am running Windows native. When i using 64 bit Java everything is OK. http://imgur.com/xgBbTAD

@binwiederhier

Ok this is indeed odd. Maybe it’s indeed a Java 32-bit-on-64 issue. If you’d like to google yourself or have advice on how to fix that, feel free to chime in. I’d have to install a 32 bit Java in my VM and investigate myself. That might take a while.

@pio2398

@binwiederhier

I think this is a general Java 32-bit on 64-bit system issue: http://stackoverflow.com/questions/19290217/using-32-bit-java-in-64-bit-windows. From the Oracle FAQs:

The maximum theoretical heap limit for the 32-bit JVM is 4G. Due to various additional constraints such as available swap, kernel address space usage, memory fragmentation, and VM overhead, in practice the limit can be much lower. On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G.

@pio2398

So in my opinion install application should give hint to install 64 bit Java (default from http://www.java.com/download is 32 bit). In case of using 32 bit Java -Xmx2048m should be removed or change to much lower valve.

@iav

I have 64 bit windows 7 and both 32 and 64 bit java, and have this bug.
Where should I write and what to run syncany?

@binwiederhier

@iav Thanks for mentioning that. A quick fix is to edit the C:Program FilesSyncanybinsy*.bat file(s) manually and set the memory limit to -Xmx1200m instead of -Xmx2048m

Regarding the overall issue: I am not quite sure how to proceed with this, because lowering the maximum memory limit in general will re-introduce #115.

The problem line is set DEFAULT_JVM_OPTS="-Xmx2048m" "-Dfile.encoding=utf-8" in the batch script; or in fact the gradle script here.

The batch file (or the installation?!) would need to detect which Java is installed/being run and set the -Xmx based on that information. I tend to agree with @portals12 here. I think the Windows installer could set this information. I see what I can do.

  • Make smarter Windows installer to detect 32-on-64 Java issue

@iav

«C:Program Files (x86)Syncanybinsy*.bat» for x64 windows.
Thank you, now it runs.

@binwiederhier

So I implemented a memory limitation through the userconfig.xml in 3f5b4a6 and 4dd7087 (comparison here: https://github.com/syncany/syncany/compare/feature/winlimitmemory)

For 32-on-64-bit Java (Wow6432Node), the installer sets <maxMemory>1300M</maxMemory> in the userconfig.xml; this value is then read by the batch script and set to -Xmx.

Can you test if it works? Can you compile it yourself or should I upload an .exe file?

@iav

Sorry, I can’t compile it myself.
And, what about usage better java version — in my case it 64 bit version?

On 22.09.2014 22:45, Philipp C. Heckel wrote:

So I implemented a memory limitation through the userconfig.xml in
3f5b4a6
3f5b4a60a84af82954b751e07d2f8a1eed845661and
4dd7087
4dd7087.

For 32-on-64-bit Java (Wow6432Node), the installer sets
|1300M|in the userconfig.xml; this value is
then read by the batch script and set to -Xmx.

Can you test if it works? Can you compile it yourself or should I
upload an .exe file?


Reply to this email directly or view it on GitHub
#222 (comment).

@binwiederhier

The Linux shell script now also reads that property (6d765be). We can’t solve the user’s problem with JAVA_HOME. If the variable is set incorrectly by the user, we can’t fix that.

@binwiederhier

@iav

Downloaded and installed.
Error is back, and inside sy.bat searching for DEFAULT_JVM_OPTS I see only -Xmx2048M (2 times)

@binwiederhier

I tested this on my Windows machine and it works perfectly fine for me — as long as we properly uninstall Syncany before: You can test if it works by trying to allocate only 1 KB of memory:

userconfig.xml

<userConfig>
   <preventStandby>false</preventStandby>
   <maxMemory>1</maxMemory>
</userConfig>

Then run

$ sy
Error occurred during initialization of VM
Too small initial heap

If this error message it thrown, the batch file uses the value in the userconfig.xml file. If not, it doesn’t. If you change it to 1300M, you should be able to run it with 32-on-64-bit Java.

I’m merging this in develop. Please confirm that it works for you, then I can close this issue.

@iav

Понравилась статья? Поделить с друзьями:
  • 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