Could not reserve enough space for object heap in java

Previous
Next

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

Cause 1

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.

Cause 2

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.

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.

Cause 3

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.

Fix 3

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

Sometimes above solutions might not work.

Another Fix

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

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_OPTIONS as the environment variable to solve this issue.

I hope this will resolve your issue with Could not reserve enough space for object heap error.

 

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog