Could not reserve enough space for object heap

I am getting the following exception repeatedly each time I try to run the program.

Error occurred during initialization of VM

Could not reserve enough space for object heap

Could not create the Java virtual machine.

I tried to increase my virtual memory (page size) and RAM size, but to no avail.

How can I eliminate this error?


Solution 1:

Run the JVM with -XX:MaxHeapSize=512m (or any big number as you need) (or -Xmx512m for short)

Solution 2:

This can also be caused by setting something too large on a 32-bit HotSpot vm, for example:

-Xms1536m -Xmx1536m

where this might/would work:

-Xms1336m -Xmx1336m

Solution 3:

here is how to fix it:

  • Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System

    Variables->New: Variable name: _JAVA_OPTIONS Variable value: -Xmx512M Variable name: Path
    Variable value: %PATH%;C:\Program Files\Java\jre6\bin;F:\JDK\bin;

Change this to your appropriate path.

Solution 4:

I ran into this when using javac, and it doesn't seem to pick up on the command line options,

-bash-3.2$ javac -Xmx256M HelloWorldApp.java 
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.

so the solution here it so set _JAVA_OPTIONS

-bash-3.2$ export _JAVA_OPTIONS="-Xmx256M"
-bash-3.2$ javac HelloWorldApp.java 
Picked up _JAVA_OPTIONS: -Xmx256M

And this compiles fine.

This happens to me on machines with a lot of RAM, but with lower memory ulimits. Java decides to allocate a big heap because it detects the ram in the machine, but it's not allowed to allocate it because of ulimits.