How does Garbage Collector work?

A Garbage Collector is a Java program which tracked the referenced (live) objects and allowed them to keep in the heap memory whereas the memory of the unreferenced (dead) objects is reclaimed and reused for future object allocation. To understand it more practically we have to take some pictorial examples of JVM object creation and management cycle. Let’s see how Garbage Collector work.

Object Creation and Memory Allocation:

When new objects are created by JVM then heap memory is allocated to each object. How? Let’s have a look at the below example.

Let’s consider the limit of Eden is 5 i.e. only 5 objects can accommodate in Eden at a time. The name of these objects as A, B, C, D and E. When these 5 objects are created, respective memory is allocated to them in Eden under the young generation.

Garbage Collector - Initial Memory Allocation
Figure 01 Memory allocation to objects

When Eden limit is over:

After some time, some of the objects become alive and some of the objects become dead. You can call the live object as ‘Referenced Object’ because those objects are referenced by some other objects and will be used in the near future. The objects who become dead are called unreferenced objects. 

In our example, you can see object A, B and E are live objects whereas object C and D are dead objects. 

Garbage Collector - Eden Limit Over
Figure 02

Minor Garbage Collection 1:

The minor garbage collection is an intermediate process which runs when Eden memory limit is over. The simple definition of Minor GC is to move the live objects from Eden to Survivor 1 (or Survivor 2) when Eden memory limit is over. There is no impact on old (Tenure) generation due to the execution of minor GC. During minor GC, 2 tasks happen:

  1. Moving the live objects from Eden to S1 or S2
  2. Remove the dead objects and reclaim the memory for new objects
  3. In the figure you can see that Object A, B and E have been moved to S1 and memory is reclaimed by removing the dead object C and D.
Garbage Collector - Minor GC 1
Figure 03: Minor GC

New Object Creation and Memory Allocation along with unreferenced objects:

One more time the first step repeats and new objects are created by JVM and heap memory is allocated to each object. This time new objects F, G, H, I and J are created and Eden memory space is allocated to them. The only difference in the first step and this step is; this time some objects are available in the S1 as well, so now we have a total of 8 objects. During this activity, some of the objects in Eden as well as in S1 may be dead like Object ‘B’ and ‘I’.

Garbage Collector - Memory Allocation and Dead Objects
Figure 04: Memory Allocation to new objects

Minor Garbage Collection 2:

Since the Eden memory is full so this situation again leads to minor GC. Here is one point to be noted minor GC is a periodic process whenever Eden memory limit is over and every time it does the same tasks which I have mentioned above. Now, the live objects F, G, H and J will move to S2 along with A and E which exist in S1. The dead objects B and I will be removed and Eden memory will be reclaimed.

Garbage Collector - Minor GC 2
Figure 06

Promotion:

There is one more process which takes place along with all the above-mentioned activities i.e. Object Promotion. After each minor GC, the objects become old and their age keeps on increasing. When aged objects reach a certain age threshold they are promoted from the young generation to the old generation. This process is called Promotion.

In our example you can see object A, G, J and O are promoted to the old generation after the ‘n’ number of minor GC executions. The promotion process leads to filling old-generation memory space. One important point to note down here some of the objects like String and Array objects are directly created in tenured generation memory space.

Object Promotion Task
Figure 07: Object Promotion

Major GC:

Major GC cleans up the old generation. The task of Major GC is as same as the minor GC, but the only difference is minor GC reclaims the memory of the young generation whereas Major GC reclaims the memory of the old generation. It is also said that many major GCs are triggered by minor GCs.

Full GC:

A Full GC is triggered whenever the heap fills up. In such a case the young generation is swept first followed by the old generation. If the old generation is too full to accept the objects of the young generation, the young generation GC is omitted and the old generation GC (major GC) is used to clean and compact the full heap, either in parallel or serial. Either way, the whole heap is collected with a stop-the-world event.

Related Term:

Stop the World Event: It is a time when all the application threads are stopped until the garbage collection operation completes. Both minor and major garbage collections are “Stop the World” events. A major GC is much slower than a minor GC because it involves all live objects.


You may be interested: