Scenario: You are not able to start the Java Virtual Machine. The JVM exits and prints the error message “Could not reserve enough space for object heap” on the Console
Facts: Upon start, the JVM prints the following error message:
Error occurred during initialization of VM. Could not reserve enough space for object heap Could not create the Java virtual machine.
What is the solution?
The Max Memory Setting of your JVM (-Xmx) is not compatible with your OS architecture/your current OS load. Here is the Action Plan:
- Check your OS architecture (is it 32-bit HotSpot ?)
- Check the current load of your machine
- Understand where you need to apply the fix
Check your OS architecture
When running a 32-bit machine the theoretical heap limit for the 32-bit JVM is 4G. However, due to additional constraints such as memory fragmentation, swap space, kernel address, in practice the limit can be much lower.
- On 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G. You can extend this value by tuning the BCDEdit and Boot.ini. See this article: https://docs.microsoft.com/en-us/windows/win32/memory/4-gigabyte-tuning
- On a Linux machine, this limit is approximately 3GB.
Here is how to check if your machine is a 32 bit machine or a 64 bit:
On a Windows machine, you can check if your machine is 32 or 64 bit as follows: Click on the Start button then choose Settings. Click on System. Under System, choose About. You will be able to see the bit-version on the System type field.
On a Linux machine, type the command “uname -m” and press “Enter”.
uname -m x86_64
That being said, if you are running a 32-bit machine, then you have to reduce the maximum amount of memory you can allocate. You can start with an high threshold value (for example 1.4 GB) and decrease by 128 MB. This will help you to find which is the hardware limit for your machine.
java -Xms128M -Xmx1408M
Besides, if you are running under the constraints of a 32-bit machine, you can consider decreasing the thread stack size. A typical thread stack size for JBoss is 128K or 256K.
java -Xss128k
Check the current load of your machine
Even though you are running a 64 bit machine, you can still have the error “Could not reserve enough space for object heap” if your heap size is larger than the physical memory available.
On a Windows machine, you can find the available Physical Memory with the systeminfo command line tool:
systeminfo |find "Available Physical Memory"
On a Linux machine you can find information about the Memory you can use the free command:
$ free total used free shared buff/cache available Mem: 65504104 5882152 47719008 680212 11902944 58207932 Swap: 8388604 0 8388604
The above command outputs the free memory (free= total – used – buff/cache) in bytes. You can use the -m parameter to display values in MB. Also the -g displays values in GB.
That being said, if the Java Max Memory value exceeds your available free memory, you have to decrease it. For example:
java -Xms1024M -Xmx4096M
Where to set the Java Max memory
When you have calculated the right amount of Java Memory for your application, it’s time to set this value. The most common option is to set the Java options on the command line, using the -Xmx parameter to set the Max Value.
On the other hand, if you are starting your Java process with Maven, you can use the MAVEN_OPTS to set the JVM options. For example:
export MAVEN_OPTS="-Xms256m -Xmx1024m"
Finally, other products such as JBoss/WildFly set the JVM settings in their shell configuration files. For example, the latest version of WildFly uses standalone.conf to set the JVM settings in JBOSS_JAVA_SIZING:
JBOSS_JAVA_SIZING="-Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m"