Hitachi

uCosminexus Application Server System Design Guide


7.15.1 Overview of G1 GC

GC is a technique that automatically reclaims memory that a program has finished using, making that memory available to other programs.

Program processing stops while GC is occurring. For this reason, appropriate implementation of GC significantly impacts the processing performance of the system.

Java objects created by a program using the new operator are stored in the memory areas managed by the JavaVM. The time period from when the Java object is created until it is no longer needed is called the lifespan of the Java object.

There are two types of Java objects, those with a short lifespan and those with a long lifespan. A Java application running on the server will create a large number of Java objects by request and response, transaction management, and other services. These Java objects have a short lifespan, as they are no longer needed when the processing ends. In contrast, the Java objects that are used continually while the application is running are considered to have a long lifespan.

To implement GC effectively, you need to reclaim memory in an efficient way by targeting objects with a short lifespan. Another key to maintaining system performance is to avoid unnecessary garbage collection for repeatedly used objects with long lifespans. An approach that achieves both these objectives is generational GC.

Generational GC manages Java objects in a New area which stores objects with a short lifespan, and a Tenured area which stores objects with a long lifespan. The New area is further divided into an Eden area for objects that were just created by the new operator, and a Survivor area for objects that have survived GC at least once. Java objects that have undergone GC in the New area a certain number of times are determined to have long-term utility and transferred to the Tenured area.

The following figure shows an overview of the memory spaces managed by generational GC and how Java objects move among them.

Figure 7‒16: Overview of memory spaces managed by generational GC and Java objects

[Figure]

There are three types of GC executed by generational GC under the G1 GC technique.

Typically, young GC and mixed GC take less time to perform than Full GC.

The following explains GC processing using the example of a specific Java object (Object A).

Processing executed in the Eden area

After creating an object A, the object A is destroyed if it is not used by the time young GC or mixed GC is first executed.

If object A is used at the time young GC or mixed GC is first executed, it is moved from the Eden area to the Survivor area.

Processing executed in the Survivor area

After young GC or mixed GC have been executed several times, object A that was moved to the Survivor area is moved from the Survivor area to the Tenured area. The threshold value used to determine whether the object is moved depends on the JavaVM options and the usage status of the Java heap. In Figure 7-16, the threshold is n times.

After moving to the Survivor area, if object A is not used when young GC or mixed GC occurs for the nth time, then object A is discarded as part of the nth young GC or mixed GC.

Processing executed in the Tenured area

If object A is moved to the Tenured area, it will not be discarded by subsequent young GC. This is because young GC targets the Eden area and Survivor area only. Mixed GC will cause some of the objects in the Tenured area to be discarded.

If the number of objects moved to the Tenured area is greater than the number of objects discarded by the mixed GC, the size of the Tenured area will increase. If mixed GC is unable to secure more space for the Tenured area, then Full GC will occur.

When tuning the JavaVM, you can prevent unnecessary objects from being moved to the Tenured area by setting the appropriate size and proportion of each memory space in the JavaVM options. This reduces the frequency with which Full GC occurs.