Garbage collection and heap expansion

Garbage collection and heap expansion are an essential part of the operation of a JVM. The frequency of garbage collection in a JVM is affected by the amount of garbage, or objects, created by the applications that run in the JVM.

Allocation failures

When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space. Garbage collection stops all other processes from running in the JVM for the duration of the garbage collection cycle, so time spent on garbage collection is time that is not being used to run applications. For a detailed explanation of the JVM garbage collection process, see z/OS User Guide for IBM SDK, Java Technology Edition, Version 7.

When a garbage collection is triggered by an allocation failure, but the garbage collection does not free enough space, the Garbage Collector expands the storage heap. During heap expansion, the Garbage Collector takes storage from the maximum amount of storage reserved for the heap (the amount specified by the -Xmx option), and adds it to the active part of the heap (which began as the size specified by the -Xms option). Heap expansion does not increase the amount of storage required for the JVM, because the maximum amount of storage specified by the -Xmx option has already been allocated to the JVM at startup. If the value of the -Xms option provides sufficient storage in the active part of the heap for your applications, the Garbage Collector does not have to carry out heap expansion at all.

At some point during the lifetime of the JVM, the Garbage Collector stops expanding the storage heap, because the heap has reached a state where the Garbage Collector is satisfied with the frequency of garbage collection and the amount of space freed by the process. The Garbage Collector does not aim to eliminate allocation failures, so some garbage collection can still be triggered by allocation failures after the Garbage Collector has stopped expanding the storage heap. Depending on your performance goals, you might consider this frequency of garbage collection to be excessive.

Garbage collection options

You can use different policies for garbage collection that make trade-offs between throughput of the application and the overall system, and the pause times that are caused by garbage collection. Garbage collection is controlled by the -Xgcpolicy option:

-Xgcpolicy:optthruput
This policy delivers high throughput to applications but at the cost of occasional pauses, when garbage collection occurs.
-Xgcpolicy:gencon
This policy helps to minimize the time that is spent in any garbage collection pause. Use this garbage collection policy with JVM servers. You can check which policy is being used by the JVM server by inquiring on the JVMSERVER resource. The JVM server statistics have fields that tell you how many major and minor garbage collection events occur and what processor time is spent on garbage collection.

You can change the garbage collection policy by updating the JVM profile. For details of all the garbage collection options, see Specifying garbage collection policy in IBM SDK, Java Technology Edition, Version 7.

Example: a multithreaded application producing large amounts of garbage

Figure 1 shows the storage heap in a JVM server at various stages for the gencon garbage collection policy. A JVM server can run many concurrent requests for the same application; an application can therefore produce larger amounts of garbage. Garbage collection in a JVM server is handled automatically by the JVM.

Figure 1. Storage heap in a JVM server with large amounts of garbage
The text in this section explains the content of the figure.

During the first 20 transactions, the active part of the storage heap starts to fill. After 80 transactions, the heap becomes full and an allocation failure occurs, which triggers a minor garbage collection in the JVM. The garbage collection cleans up the short lived objects. However, because application requests are still running, some of the objects are still referenced, so they are not eligible for garbage collection.

After 100 transactions, the Garbage Collector cannot find enough space for all the currently needed objects and it expands the storage heap . Some storage from the maximum amount of storage reserved for the storage heap (the amount specified by the -Xmx option) is added to the active part of the heap. The application continues to produce objects, but the heap expansion has now created enough space so that the current transaction can complete.

After 110 transactions, the storage heap is largely occupied. Another allocation failure occurs that triggers a major garbage collection. The JVM cleans up many of the longer lived objects used by previous transactions. After another 20 transactions, the heap starts to fill up again.

When you use the gencon policy, many minor garbage collections can occur to manage the heap size before a major garbage collection occurs. You can find out how many garbage collections have occurred, the heap occupancy, and other information by using JVM server statistics.



dfht3_jvm_gc.html | Timestamp icon Last updated: Monday, 5 June 2017