Learnitweb

Generations and generational garbage collection in Java

1. Introduction

In this tutorial, we’ll discuss how garbage collector works. We’ll also discuss generations and generational garbage collection process.

An object is considered to be eligible to be collected by garbage collector when it can no longer be reached from any reference of any live object in the running program. The memory of any garbage object which is collected by the garbage collector is reused by the VM.

The Java HotSpot VM incorporates different garbage collection algorithms. All these algorithms except Z Garbage Collector (ZGC) use a technique called generational collection.

This generational collection is based on a hypothesis called weak generational hypothesis. This hypothesis states that most objects survive for a short period of time.

Based on the lifetime, objects usually are of three types:

  • Short lived objects. For example, iterator objects are alive only for the duration of the loop.
  • Objects having life neither very short or long. For example, objects used in intermediate computations.
  • Long lived objects which exists until VM exits like the application property objects which are required throughout the life of application.

2. Generations

As mentioned earlier, objects in JVM have different age. The objects reside in memory pool. The memory is managed in generations. Here, generations are the memory pools holding objects of different ages. When generation fills up, garbage collection occurs in that generation.

memory pool generations in Java

The entire address space including the Java heap is logically divided into young and old generations.

At the initialization of the virtual machine, the entire space for the heap is reserved. The maximum size reserved can be specified with -Xmx option and the minimum size reserved is specified -Xms option. If the value of -Xms is smaller than the -Xmx the entire space that is reserved is not committed immediately. The uncommitted space is shown as virtual in the image.

3. Minor and Major collection

As mentioned earlier, most objects survive for a short period of time. The memory pool allocated to these young objects is called the young generation. Most objects are allocated to this young generation. When the young generation is filled with objects, garbage collection occurs and this collection is called as minor collection. In minor collection, collection occurs only in young generation. No object in other generation is collected.

In minor collection, objects which are still in use are not collected. These are called surviving objects. These surviving objects are moved to old generation in every minor collection. When old generation fills up, garbage collection occurs. This is called as major collection. In major collection, entire heap is collected and usually last longer than minor collection as memory and objects involved are large in comparison to minor collection.

4. How garbage collection occur in generations?

The process of garbage collection can be explained with the help of following points:

  • The young generation consists of eden and two survivor spaces (see the above diagram).
  • The objects to be designated to young generation are initially allocated in eden.
  • At any time, one of the two survivor space is empty.
  • Live objects are allocated in one survivor space and the other survivor space is empty. When garbage collection occurs, the objects of one survivor space are moved to other empty survivor space. So, after garbage collection, eden and one survivor space is empty.
  • In the next garbage collection, objects are collected from eden and moved from the filled survivor space to empty survivor space.
  • Objects are copied from one survivor space to another until they have been copied certain number of times or there is no empty space left. This process is called aging.

The command-line option -verbose:gc prints information about the heap and garbage collection at each collection.

5. Factors affecting garbage collection

The two most important factors which affect the garbage collection are:

  1. Total available memory
  2. Proportion of heap dedicated to the young generation.

The larger the memory, larger will be the time taken for collection. Since most of the objects are short lived and usually collected in the young generation, the larger the memory allocated for young generation, larger the time for garbage collection. When garbage collector runs, it stops the application for some time when it collected and reallocates the memory to the virtual machine. This impacts the throughput of the application.

So we must consider these two factors which tweaking the parameters for memory allocation.