6.4.4 Expansion and contraction

There is a cost associated with expanding and shrinking the heap because processor time is spent in the overhead involved with paging. It is good policy to reduce the amount of times the heap changes size by monitoring the heap size and tuning accordingly. The ideal situation is to have a heap that never changes size. But if the heap does change size significantly and often during an application's lifetime, then it is worth the effort to tune heap expansion and shrinkage policies. Additionally, it is prudent at this time to investigate whether increasing the page size will help application performance. To determine whether increasing the page size will help you application, see 6.6, Using large page sizes in this chapter.

Tuning the heap expansion and shrinkage policies will improve an application's performance by reducing the amount of time spent paging. Table 6-5 describes heap expansion and contraction runtime options. The design specifics of heap expansion and shrinkage are explained fully in IBM Java 5 Diagnostics Guide.

The following runtime options are used to set the heap expansion and shrinkage policies:

-Xminf and -Xmaxf set the free space after collection.

-Xmine and -Xmaxe set the expansion amount.

-Xmint and -Xmaxt set the GC collection time threshold.

Example 6-3 shows what an expansion of the heap looks like in verbosegc. Note that the expansion will be contained within an allocation failure or possibly some other GC event.

Example 6-3 Verbosegc output showing heap expansion

...
<expansion type="tenured" amount="3146752" newsize="7341056" timetaken="0.139" reason="satisfy allocation request" />
...

Example 6-4 shows what a contraction of the heap looks like in verbosegc.

Example 6-4 Verbosegc output showing heap contraction

...
<contraction type="tenured" amount="5327872" newsize="101239808" timetaken="0.020" reason="excess free space following gc" />
...

Example 6-5 shows a default heap expansion which results in 30% free heap.

Example 6-5 Verbosegc output of default heap expansion event

<af type="tenured" id="5" timestamp="Wed Oct 11 10:53:49 2006" intervalms="409.840">
  <minimum requested_bytes="4120" />
  <time exclusiveaccessms="0.485" />
  <tenured freebytes="0" totalbytes="23528448" percent="0" >
    <soa freebytes="0" totalbytes="21411840" percent="0" />
    <loa freebytes="0" totalbytes="2116608" percent="0" />
  </tenured>
  <gc type="global" id="5" totalid="5" intervalms="409.937">
    <expansion type="tenured" amount="9783296" newsize="33311744" timetaken="8.881" reason="insufficient free space following gc" />
    <refs_cleared soft="0" weak="3" phantom="0" />
    <finalization objectsqueued="2" />
    <timesms mark="19.728" sweep="0.265" compact="0.000" total="28.949" />
    <tenured freebytes="9999056" totalbytes="33311744" percent="30" >
      <soa freebytes="9999056" totalbytes="33311744" percent="30" />
      <loa freebytes="0" totalbytes="0" percent="0" />
    </tenured>
  </gc>
  <tenured freebytes="9994936" totalbytes="33311744" percent="30" >
    <soa freebytes="9994936" totalbytes="33311744" percent="30" />
    <loa freebytes="0" totalbytes="0" percent="0" />
  </tenured>
  <time totalms="29.517" />
</af>

Table 6-5 lists and describes the expansion and contraction runtime options for heap.

Table 6-5

Runtime option Description
-Xmine<size> Sets the minimum amount by which the garbage collector expands the heap. Typically, the garbage collector expands the heap by the amount required to restore the free space to 30% (or the amount specified using -Xminf). The -Xmine option sets the expansion to be at least the specified value. For example, -Xmine50M sets the expansion size to a minimum of 50 MB. By default, the minimum expansion size is 1 MB.
-Xminf<size> Specifies the minimum percentage of heap that should be free after a garbage collection. If the free space falls below this amount, the JVM attempts to expand the heap. Specify the size as a decimal value in the range 0-1; for example, a value of -Xminf0.3 requests the minimum free space to be 30% of the heap. By default, the minimum value is 0.3.
-Xmaxe<size> Sets the maximum amount by which the garbage collector expands the heap. Typically, the garbage collector expands the heap when the amount of free space falls below 30% (or the amount specified using -Xminf), by the amount required to restore the free space to 30%. The -Xmaxe option limits the expansion to the specified value. For example, -Xmaxe10M limits the expansion to 10 MB. By default, there is no maximum expansion size.
-Xmaxf<size> Specifies the maximum percentage of heap that must be free after a garbage collection. If the free space exceeds this amount, the JVM attempts to shrink the heap. Specify the size as a decimal value in the range 0-1. For example, -Xmaxf0.5 sets the maximum free space to 50%. The default value is 0.6 (60%).
-Xmint<value> Specifies the minimum percentage of time which should be spent in Garbage Collection. If the percentage of time drops below this value, the JVM attempts to shrink the heap. Specify the percentage as a decimal in the range 0-1. For example, -Xmint0.1 sets the minimum time spent in Garbage Collection to 10%. The default value is 5%.
-Xmaxt<value> Specifies the maximum percentage of time which should be spent in Garbage Collection. If the percentage of time rises above this value, the JVM attempts to expand the heap. Specify the percentage as a decimal in the range 0-1. For example, -Xmaxt0.2 sets the maximum time spent in Garbage Collection to 20%. The default value is 13%.

Heap expansion and contraction runtime options