Determine initial heap size | Expansion and contraction
6.4.3 Determine 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%.
... <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, for example, more than once every 10 minutes, while also not being so large that collections become too costly a procedure, because collections take longer on larger heaps.
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.
To determine an optimal maximum heap size...
- 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.
- Monitor the application for performance. Use these numbers as a baseline.
- At the same time, determine the allocation failure rate from analyzing verbosegc output.
- Increase the maximum heap size if allocation failures are quite frequent.
- 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.