Java – Why is heap size fixed on JVMs

java

Can anyone explain to me why JVMs (I didn't check too many, but I've never seen one that didn't do it that way) need to run on a fixed heap size? I know it's easier to implement on a simple contiguous heap, but the Sun JVM is now over a decade old, so I'd expect them to have had time to improve this.

Needing to define the maximum memory size of your program at startup time seems such a 1960s thing to do, and then there are the bad interactions with OS virtual memory management (GC retrieving swapped out data, inability to determine how much memory the Java process is really using from the OS side, huge amounts of VM space wasted (I know, you don't care on your fancy 48bit machines…)). I also guess that the various sad attempts to build small operating systems inside the JVM (EE application servers, OSGi) are at least partially to blame on this circumstance, because running multiple Java processes on a system invariably leads to wasted resources because you have to give each of them the memory it might have to use at peak.

Surprisingly, Google didn't yield the storms of outrage over this that I would expect, but they may just have been buried under the millions of people finding out about fixed heap size and just accepting it for a fact.

Best Answer

You are wrong. The heap size of JVMs is not fixed, only bounded:

  • -Xmx sets the maximum heap memory size
  • -Xms sets the minimum heap memory size

Setting an upper limit is necessary for several reasons. First, it tells the garbage collector when to spring into action. Second, it prevents the JVM from clogging the whole machine by consuming too much memory. The minimum heap size is probably useful to reserve the ammount of memory the program needs at least, to prevent it running out of memory (because other processes consume too much).

Related Topic