3

After readings How many characters can a Java String have? I started wonder wonder:

Why is there are max heap setting in current JVMs? Why not just request more memory from the operating system when heap memory runs out and the garbage collector was unable to free needed memory? Does anybody know the rationale behind it?

Community
  • 1
  • 1
Martin
  • 11,577
  • 16
  • 80
  • 110

7 Answers7

3

I believe that it helps sandbox the Java programs ie stops them taking all the memory on the physical machine. Also Memory Leaks can still happen in Java even with garbage collection, and they can be even more subtle than in C/C++ at times.

If you really need a bigger memory allowance for your Java software you can tweek the max VM size in the config files.

Graeme Smyth
  • 136
  • 5
  • Right: Java can run inside a WebBrowser and you don't want the sand-boxed Java to take over the system. So I take this as the most likely reason behind the decision. Most other described scenarios could have been fixed with a more intelligent garbage collector as well. – Martin Dec 10 '10 at 10:41
2

Because you don't want the JVM taking over every possible resource on your O/S (well in some cases you do, but in that case set the Max Heap size to be the max your JVM, O/S combo can handle).

Martijn Verburg
  • 3,287
  • 21
  • 26
2

The JVM is a virtual machine. When you create a virtual machine, you limit its resources, since typically you want more than one virtual machine on an actual machine.

1

Because too much heap memory can actually be detrimental - you could have a situation where a relatively small application uses all of a large heap so that when GC does kick in it brings your app to a halt while it reclaims oodles of memory.

A smaller heap would allow gc to run more often, but not cripple your program each time.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
  • But is suggested that new memory is only requested after the GC already had a run. Also: Why should a GC only run when memory is low? Would it be not better to run the GC when the system is idle? Especially when you use a moving collector. With some operating systems a moving collector could even returned freed memory to the OS. – Martin Dec 08 '10 at 10:07
1

Because physical memory is limited too. You cannot request more and more. Actually you can and OS will allocate the virtual memory even if the physical RAM is unavailable. The memory will be allocated on disk that may cause serious performance problems. In worse case the whole computer got stuck and you cannot do anything with it. It is better to fail earlier, i.e. better that JVM crashes than the physical host got stuck.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

Two things:

  1. you don't want all your resources to be consumed
  2. a too high heap size will cause problems with garbage collector, because your program might pause for a few minutes unexpectedly because the GC must do it's job

Problem 2. is large enough for situations where hundreds of GBs are allocated for big Java processes and even alternative solutions like Terracotta's bigmemory have been developed.

darioo
  • 46,442
  • 10
  • 75
  • 103
0

Maybe you don't want your whole memory to be used by only one application. Notice, that memory may not be released if there is still some free memory, it may mean, that if you have application perfectly running with Xmx200m and run it on no max heap limit, it could take whole memory.

Mirek Pluta
  • 7,883
  • 1
  • 32
  • 23