A number of parameters affect generation size. The following diagram illustrates the difference between committed space and virtual space in the heap. At initialization of the virtual machine, the entire space for the heap is reserved. The size of the space reserved can be specified with the -Xmx
option. If the value of the -Xms
parameter is smaller than the value of the -Xmx
parameter, not all of the space that is reserved is immediately committed to the virtual machine. The uncommitted space is labeled "virtual" in this figure. The different parts of the heap (permanent generation, tenured generation and young generation) can grow to the limit of the virtual space as needed.
By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range. This target range is set as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum>
and -XX:MaxHeapFreeRatio=<maximum>
, and the total size is bounded below by -Xms<min>
and above by -Xmx<max>
.
Parameter Default Value
MinHeapFreeRatio 40
MaxHeapFreeRatio 70
-Xms 3670k
-Xmx 64m
Default values of heap size parameters on 64-bit systems have been scaled up by approximately 30%. This increase is meant to compensate for the larger size of objects on a 64-bit system.
With these parameters, if the percent of free space in a generation falls below 40%, the generation will be expanded to maintain 40% free space, up to the maximum allowed size of the generation. Similarly, if the free space exceeds 70%, the generation will be contracted so that only 70% of the space is free, subject to the minimum size of the generation.
Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications. The rules of thumb for server applications are:
- Unless you have problems with pauses, try granting as much memory as
possible to the virtual machine. The default size (64MB) is often too
small.
- Setting -Xms and -Xmx to the same value increases predictability by
removing the most important sizing decision from the virtual machine.
However, the virtual machine is then unable to compensate if you make
a poor choice.
In general, increase the memory as you increase the number of
processors, since allocation can be parallelized.
There is the full article