1

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?

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

1 Answers1

1

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.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Thanks. Anyway to find out how much continuous memory you have left on Windows? – Yahya Uddin Jul 20 '19 at 22:33
  • @YahyaUddin None that I've found, sadly. Seems the only way is to write native code to allocate and free various blocks of memory until you hit the limit. – Michael Berry Jul 20 '19 at 22:38
  • What I can't understand is how this correlates with the notion of virtual memory. Could someone please explain that to me? OS might swap out that JVM's memory pages to disk, then load them again into RAM - chances are they will be loaded into different locations, so physical memory used by the process is no longer contiguous. As for the process's virtual memory - that will always be contiguous, as each process has its own address space. Or am I missing something? – Nikita Tkachenko Apr 12 '20 at 16:39