1

I have a Windows 7 laptop, with 16GB RAM. When I run multiple java servers I get OutOfMemory error, however, windows task manager shows that I still have 6GB of free physical memory left. It seems like to me that my JVM can only allocate ~2GB heap for all running java apps.

So I wrote some very simple Java code:

public class HelloWorld {
    public static void main(String[] args) {
         for(;;) System.out.println("Hello, World");
    } 
}

My -jvm version:

$ java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

JAVA_HOME "C:\Program Files\Java\jdk1.8.0_111"

JRE_HOME "C:\Program Files\Java\jre1.8.0_111"

I did a simple java -Xmx256m -Xms256m HelloWorld and it worked as expected, however when I switched to java -Xmx2048m -Xms2048m HelloWorld I get the following:

Error occurred during initialization of VM
Could not reserve enough space for object heap

I tried multiple JDKs but none of them are working. Both windows and JVM are in 64 bits.

What can I do to fix this problem?

Bob
  • 11
  • 2
  • Possible duplicate of [“Error occurred during initialization of VM; Could not reserve enough space for object heap” using -Xmx3G](http://stackoverflow.com/questions/9303889/error-occurred-during-initialization-of-vm-could-not-reserve-enough-space-for). Specially, see this answer http://stackoverflow.com/a/9304270/529630 – Dunes Dec 02 '16 at 22:31
  • do you have swap enabled? windows' memory accounting is different compared to linux. – the8472 Dec 04 '16 at 08:28

2 Answers2

1

It seems like to me that my JVM can only allocate ~2GB heap for all running java apps.

That is not correct. The JVM is capable of asking for and using a >2GB heap.

It must be that Windows is restricting what the Java process can ask for. It may also be that Windows has reserved memory for multiple Java processes, but the memory is showing as free because the JVMs have not used it.

FWIW, running JVMs with lots more memory than they really need is not a good idea.

The fix?

  1. Don't do it. Don't run lots of 2GB Java servers on your laptop, or reduce the heap size to something more reasonable. Overcommitting your memory is an attractive idea, until the JVMs start using all of the memory that you have allocated them. Then your system thrashes ...

  2. Do it differently. You can run multiple servers in a single JVM.

  3. Get more memory.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Just don't use -Xms2048m. It makes JVM to request a continuous 2Gb block of memory at once. Let JVM decide when to get additional mem for heap needs. In case of Windows, the effect you observe may be the result of badly aligned dlls mapped to proc virtual memory (antivirus, firewall, rootkit, virus etc), which make virtual memory fragmented. Running java from ide, using agents (with native impl) could also add hidden content to process virtual memory. A nice tool to observe a virtual memory of a process is vmmap from Microsoft Sysinternals suite

IrLED
  • 438
  • 2
  • 5