Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to specify the Java heap size?

I have always wondered, why does Java require you set the heap size manually? I was under the impression that programs written in other languages would just allocate as much memory as needed as the program run until the OS could allocate no more.

In the Java world we need to set the heap, stack, and permgen size. While this is simple enough forgetting to increase these to "large enough" numbers is the #1 reason I have seen servers go down.

Why is it not possible to tell Java to grow the heap/stack/permgen as much as it needs over time?

like image 923
benstpierre Avatar asked Aug 18 '10 21:08

benstpierre


People also ask

What is the use of heap size in Java?

Heap space is used for the dynamic memory allocation of Java objects and classes at runtime. New objects are always created in the heap space, and references to these objects are stored in the stack memory.

What does heap size do?

A larger heap size also means that a larger amount of time is needed to find and process objects that need to be collected. When you plan for system memory consumption, include additional processor memory for the JVM to use outside of the heap size and random access memory (RAM) for the operating system.

Does heap size affect performance?

A too small heap size may affect performance if your system also does not have enough cores, so that the garbage collectors do compete over the CPU with the business threads. At some point, the CPU spends a significant time on garbage collection.

What should my heap size be?

Initial heap size is 1/64th of the computer's physical memory or reasonable minimum based on platform (whichever is larger) by default. The initial heap size can be overridden using -Xms. Maximum heap size is 1/4th of the computer's physical memory or 1 GB (whichever is smaller) by default.


2 Answers

Three reasons:

  1. Because Java was intended to be a language for writing web apps. It is generally not thought to be a good idea to let a web app take over all of the machine resources.
  2. Because Java is garbage collected. As well as specifying an upper limit of memory, the heap size triggers garbage collection too. Essentially you are saying "you can use this much, but when you hit that limit you've got to tidy up".
  3. There are many classes of applications where you do want to limit the amount of memory used by the Java process, e.g. where it is more important that other processes continue when there is insufficient memory for both.
  4. Because being able to limit heap size is a feature. If you like the approach where the process can have unlimited memory, (as on the other languages mentioned) then you can always set the heap limit to be more than it could possibly acquire. If you want to limit heap size, you can do that. Other languages have only one of those possible behaviours, making them less flexible.
like image 138
DJClayworth Avatar answered Sep 20 '22 15:09

DJClayworth


My interpretation is that Sun is a company focused on selling Big Boxes and they like that the sysadmins can do things on these boxes. Running a system to fill all the memory is a good way to run it into the ground and be unable to efficiently take action to restore operation because you cannot allocate memory to create a login shell for example.

like image 39
Peter Tillemans Avatar answered Sep 22 '22 15:09

Peter Tillemans