How does Garbage Collector work?

In our previous article on the Garbage Collector, we explained what a Garbage Collector is and why it is important in Java. We covered the basic concept that Java automatically removes objects from memory when they are no longer needed, helping prevent memory leaks and improving application performance.

In this article, we’ll examine how the Garbage Collector works. To understand it more practically, we have to take some pictorial examples of the JVM object creation and management cycle. So, let’s see how the Garbage Collector works.

Object Creation and Memory Allocation:

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

Java uses heap memory divided into:

  • Young Generation, which includes:
    • Eden (where new objects are created)
    • Two Survivor spaces (S1 and S2)
  • Old Generation (for longer-lived objects)

Example: Let’s consider the limit of Eden is 5 i.e. only 5 objects can be accommodated in Eden at a time. The names 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 the 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 soon. The objects that 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 that runs when the 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 the Eden memory limit is over. There is no impact on the 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 Objects A, B, and E have been moved to S1, and memory is reclaimed by removing the dead objects 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 the JVM, and heap memory is allocated to each object. This time, new objects F, G, H, I, and J are created, and the 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 the 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 the Eden memory will be reclaimed.

Garbage Collector - Minor GC 2
Figure 06

Promotion:

There is one more process that 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 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 that objects 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 here some of the objects, like String and Array objects, are directly created in the 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 that 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.

Quick Recap

StepWhat Happens
Initial allocationA, B, C, D, E placed in Eden
Eden fillsA, B, E are live; C, D are dead
Minor GC #1Move A, B, E to Survivor; delete C, D
New objects addedF, G, H, I, J into Eden
Eden fills againMinor GC #2: move live, delete dead
PromotionA, G, J (aged enough) to Old Generation
Major/Full GC triggeredClean dead objects in Old Gen (and maybe Young Gen), pause all threads

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: