Search code examples
javajvmjavac

Java: "Could not reserve enough space for object heap" error, despite enough memory


When running a command like:

$ javac -J-Xmx1072M ...

everything works fine.

When running the same command with larger memory:

$ javac -J-Xmx2048M ...

I get the following error:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.

However, I do not understand why, because when I run systeminfo in my windows command prompt, I get this info:

Total Physical Memory:     16,315 MB
Available Physical Memory: 6,351 MB
Virtual Memory: Max Size:  32,187 MB
Virtual Memory: Available: 13,666 MB
Virtual Memory: In Use:    18,521 MB

Meaning, I have plenty of physical memory left over.

What seems to be the issue?


Solution

  • The JVM can't just pick any old memory anywhere for its object heap, it has to be contiguous; meaning a continuous, unfragmented block of free memory. While you theoretically might have enough raw memory free to launch a JVM with this heap size, if it's not contiguous then it's useless as far as the JVM is concerned.

    Note that this is far, far more likely to happen with a 32 bit address space (if you're using a 32 bit OS or 32 bit JVM), but can of course still happen regardless.