How does a Garbage collector work?

A garbage collector is a Java program that keeps track of referenced (alive) objects and allows them to remain in heap memory while reclaiming memory from unreferenced (dead) objects and reusing them. for further assignment of objects. We need to use some visual examples of the JVM object generation and management cycle to understand it better. Let’s take a look at how the garbage collector works.

Memory allocation and object creation

Heap memory is allocated to each new object as it is created by the JVM. As? Let’s see the example below.

Let’s say Eden has a capacity limit of 5, which means that only five items can be present at a time. These items are labeled A, B, C, D, and E. When each of these five objects is created, Eden’s younger generation allocates the appropriate amount of memory to it.

Figure 01 Memory allocation to objects

After the edge of Eden:

After a while, some elements come to life while others disappear. Since they will soon be used and referenced by other objects, you can refer to living things as referenced objects. Unreferenced objects are objects that cease to exist.

As you can see in our example, items A, B, and E are still alive, while items C and D are dead.

Figure 02

Minor Garbage Collection 1

When Eden’s memory limit is reached, a transition process known as small garbage collection begins. When Eden’s memory limit is reached, Minor GC simply means moving living creatures from Eden to Survivor 1 (or Survivor 2). Performing minor GC does not affect the previous generation (Holding). Minor GC includes two tasks:

  1. S1 or S2 after moving Eden’s living items
  2. Reclaim RAM for new stuff by deleting old objects.
  3. As shown in the image, objects A, B, and E were moved to S1, and memory was freed by deleting useless objects C and D.

Figure 03: Minor GC

Memory allocation for unreferenced elements and creation of new objects:

The first step is repeated one more time, and this time the JVM creates new objects and allocates a bunch of memory to each of them. This time, Eden’s memory space is allocated to the new objects F, G, H, I, and J. The only difference between the first step and this step is that there are now 8 objects in total because some of them are now also available in the S1- step. Some objects in Eden and S1 can die during this activity, including objects “B” and “I”.

Figure 04: Memory Allocation to new objects

Minor Garbage Collection 2

Since Eden’s memory is already full, this circumstance results in another small VC. One thing to note is that the little GC will periodically perform the same activities I mentioned above when Eden’s memory limit is reached. Along with A and E, which are currently on S1, active items F, G, H, and J are now transferred to S2. B and I, the dead objects, will be removed and the memories of Eden will be restored.

Figure 05

Promotion:

In addition to all the processes mentioned above, the promotion of items also takes place. Things are getting old and their age continues to increase with each little GC. Items are promoted from the younger generation to the older generation when they reach a certain age limit. Promotion is the name of this process.

In our example, you can see that after ‘n’ minor GC runs, objects A, G, J, and O are promoted to the previous generation. The promotion procedure causes the memory space of the previous generation to fill up. One thing to note is that certain objects, such as String and Array objects, occur directly in the memory space of the incumbent generation.

Figure 06: Object Promotion

Major GC:

Major GC cleans up the previous generation. Major GC performs the same task as minor GC; the only difference is that the small GC restores the memories of the younger generation, while the large GC restores the memories of the older generation. According to another statement, numerous large WCs are caused by mild WCs.

Full GC:

When the heap is full, a full GC is used. The older generation (large GC) is used to clean and compact the entire heap, in parallel or serial, while the younger generation (GC) is skipped. In both cases, a world-stop event picks up the entire deck.

Related Term:

Stop the World Event: This occurs when all application threads are suspended until the garbage collection process is complete. Garbage collections, both small and large, are occasions to “Stop the World.” Since all living elements are involved in a large GC, it is much slower than a small GC. has begun. In this situation, the generation is first removed Don’t take what the younger generation offers,

Scroll to Top