7.2.3 Mechanism of copy GC
In JavaVM, the memory space of the New area for which copy GC is executed, is divided into the Eden area and the Survivor area. The Survivor area is furthermore divided into the From space and the To space. The From space and the To space have the same memory size.
The following figure shows the configuration of the New area:
|
|
An Eden area is the area where the objects created using new are stored initially. When new is executed in a program, memory from the Eden area is allocated.
When the Eden area becomes full, copy GC is executed. The following processing is performed in copy GC:
-
Among the Java objects existing in the Eden area and the From space of the Survivor area, the objects that are in use are copied to the To space of the Survivor area. The Java objects that are not in use are destroyed.
-
The To space and the From space of the Survivor area are swapped.
As a result, the Eden area and the To space become empty, and the objects that are in use remain in the From space.
The following figure shows the movement of objects when copy GC is executed:
|
|
In this way, when copy GC occurs, the objects that are in use move to and fro between the From space and the To space. If the objects with a long lifespan continue to move to and fro, however, copy processing becomes an overhead. To prevent this, set a threshold value for the frequency of switching the Java objects between the From space and the To space, so that the Java objects whose age has reached the threshold value move to the Tenured area.