6.4.3 Determining maximum heap size
The conventional wisdom for determining the maximum heap size has been to base it on percentage utilization, usually about 50%. For example, a running application would show the percent freebytes as 50%. Example 6-2 shows this scenario.
Example 6-2 Sample line from verbosegc showing 50% utilization
...<tenured freebytes="53276072" totalbytes="104857600" percent="50" >...
![]()
However, this approach does not take into account the purpose in sizing the heap. The goal is to have a heap large enough to avoid too many allocation failures, while also not being so large that collections become too costly a procedure, because collections take longer on larger heaps. Also, the possibility of the heap becoming fragmented exists if the heap is so large that no collection occurs for a long period of time. When an allocation failure occurs when the heap is heavily fragmented, the collection will require a compaction that could take an unacceptably long time.
Note: Keep in mind the goal when determining the maximum heap size. It should be large enough so that the number of allocation failures is minimal, but yet small enough so that collections do not become too costly. Larger heaps run the risk of becoming fragmented which can in turn lead to costly compactions.
The correct approach to take is to analyze verbosegc output to see what the collector is doing, and then size to optimize the GC operations.
To determine an optimal maximum heap size, perform the following steps:
1. Start with a size that is at least 30% larger than the maximum heap used, or 50% larger than the average heap utilized, if the heap tends to fluctuate in size.
2. Monitor the application for performance. Use these numbers as a baseline.
3. At the same time, determine the allocation failure rate from analyzing verbosegc output.
4. Increase the maximum heap size if allocation failures are quite frequent.
5. Decrease the maximum heap size if allocation failures are infrequent, but cause pause times to be unacceptably too long. A smaller maximum heap size also will be necessary if, during an extended run, the heap becomes fragmented. It must be said that it is impossible perform test runs that perfectly mimic application usage. So how can you size the heap on a production system? The answer is to monitor both application performance and the allocation failure rate on the production server. Try to keep the allocation failure rate as low as possible (for example, once every ten minutes or more).