4 Sizing the Generations
A number of parameters affect generation size. Figure 4-1, “Heap Parameters” 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, than 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 (tenured generation and young generation) can grow to the limit of the virtual space as needed.
Figure 4-1 Heap Parameters
Total Heap
The following discussion regarding growing and shrinking of the heap and default heap sizes does not apply to the parallel collector. However, the parameters that control the total size of the heap and the sizes of the generations do apply to the parallel collector.
The most important factor affecting garbage collection performance is total available memory. Because collections occur when generations fill up, throughput is inversely proportional to the amount of memory available.(这里的throughput指程序运行的一段时间内,没有花在垃圾回收上的时间的占比。吞吐量与可用的内存量成反比,即由于堆内存小的时候垃圾收集速度快,所以这时的throughput比较高)
🍐 -XX:MinHeapFreeRatio=<minimum>
🍐 -XX:MaxHeapFreeRatio=<maximum>
🍐 -Xms<min>
🍐 -Xmx<max>
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>
- Table 4-1 Default Parameters for 64-Bit Solaris Operating System
Parameter | Default Value |
---|---|
MinHeapFreeRatio | 40 |
MaxHeapFreeRatio | 70 |
-Xms | 6656k |
-Xmx | calculated |
With these parameters, if the percent of free space in a generation falls below 40%, then 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%, then the generation will be contracted so that only 70% of the space is free, subject to the minimum size of the generation.
The following are general guidelines regarding heap sizes for server applications:
- Unless you have problems with pauses, try granting as much memory as possible to the virtual machine. The default size 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.
The Young Generation
The bigger the young generation, the less often minor collections occur. However, for a bounded heap size, a larger young generation implies a smaller tenured generation, which will increase the frequency of major collections. The optimal choice depends on the lifetime distribution of the objects allocated by the application.
🍐 -XX:NewRatio
🍐 -XX:MaxNewSize
By default, the young generation size is controlled by the parameter NewRatio. For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be 1/4 of the total heap size.
The parameters NewSize and MaxNewSize bound the young generation size form below and above. Setting these to the same value fixes the young generation, just as setting -Xms and -Xmx to the same value fixes the total heap size. This is useful for tuning the young generation at a finer granularity than the integral multiples allowed by NewRatio.
Survivor Space Sizing
🍐 -XX:SurvivorRatio
SurvivorRatio can be used to tune the size of the survivor space, but this is often not important for performance.
For example, -XX:SurvivorRatio=6 sets the ratio between eden and a survivor space to 1:6. In other words, each survivor space will be 1/6 the size of eden, and thus 1/8 the size of the young generation (not 1/7, because there are 2 survivor spaces).
If survivor spaces are too small, copying collection overflows directly into the tenured generation. If survivor spaces are too large, they will be uselessly empty. At each garbage collection, the virtual machine chooses a threshold number, which is the number times an object can be copied before it is tenured (这个阈值是对象在移动到老年代之前被复制的次数). This threshold is chosen to keep the survivors half full. The command line option -XX:+PrintTenuringDistribution (not available on all garbage collectors) can be used to show this threshold and the ages of objects in the new generation. It is also useful for observing the lifetime distribution of an application.
Table 4-2 Default Parameter Values for Survivor Space Sizing
Parameter | Server JVM Default Value |
---|---|
NewRatio | 2 |
NewSize | 1310M |
MaxNewSize | not limited |
SurvivorRatio | 8 |
The maximum size of the young generation will be calculated from the maximum size of the total heap and the value of the NewRatio parameter. The “not limited” default value for the MaxNewSize parameter means that the calculated value is not limited by MaxNewSize unless a value for MaxNewSize is specified on the command line.
The following are general guidelines for server applications:
- First decide the maximum heap size you can afford to give the virtual machine. Then plot your performance metric against young generation sizes to find the best setting.
- Note that the maximum heap size should always be smaller than the amount of memory installed on the machine to avoid excessive page faults and thrashing.
- If the total heap size is fixed, then increasing the young generation size requires reducing the tenured generation size. Keep the tenured generation large enough to hold all the live data used by tha application at any given time, plus some amount of slack space (10 to 20% or more)
- Subject to the previously stated constraint on the tenured generation:
- Grant plenty of memory to the young generation.
- Increase the young generation size as you increase the number of processors, because allocation can be parallelized.