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
- Cause 1: Did not specify heap size
- Cause 2: Too large Xmx value
- Cause 3: Specifying large heap size more than physical memory
- Could not reserve enough space for 2097152kb object heap
error occurred during initialization of vm could not reserve enough space for 2097152kb object heapis 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 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.
- 2^32 (~4GB) on 32 bit JVM
- 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.
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
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.
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.
Let’s say you are getting an error with below execution.
java -Xms1536M -Xmx1536M MyApplication
You might not have enough contiguous free space in memory.You can run the application with slightly lower heap size to resolve the issue.
java-Xms1336M -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.
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
You can run the application with heap size which is less than your physical memory.
java-Xms2048M -Xmx2048M MyApplication
Sometimes above solutions might not work.
Set _JAVA_OPTIONS environment variable
So you can set
_JAVA_OPTIONS as the environment variable.
-bash-3.2$ export _JAVA_OPTIONS ="-Xmx512M"
-bash-3.2$ javac MyApp.java
Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System
Variables->New: Variable name:
Variable value: -Xmx512M
💡 Did you know?
JDK_JAVA_OPTIONSis prefered environment variable from
Java 9+onward to specify Java options. It will be ignored in the version lesser than Java 9.
Could not reserve enough space for 2097152kb object heap
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 is a mobile application development framework originally created by Nitobi.
If you are getting this error on Apache cordova, here are solutions.
- Switch from 32 bit JVM to 64 bit JVM
- set _JAVA_OPTIONS environment variable with
If you are getting this error, while launching Minecraft game, then you need to switch from 32 bit JVM to 64 bit JVM.
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
set JAVA_OPTIONS=-server -Xms512m -Xmx2g -Xss256k -XX:+UseG1GC
set JAVA_OPTIONS=-server -Xms512m -Xmx1024m -Xss256k -XX:+UseG1GC
This should resolve this error for JFrog Artifactory installation.
- 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_OPTSas the environment variable to solve this issue. If you set
JAVA_OPTSenvironment 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.