Garbage Collection – Overview

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. This method of reclaiming unused memory is known as Garbage Collection. In Garbage collection, neither the explicit deletion of the memory happens nor the memory is given back to the operating system. As long as an object is being referenced, the JVM considers it alive and keeps it in the memory. Once the object is no longer referenced (dead) or not reachable by the application code then the garbage collector removes it and reclaims the unused memory. This process is also called automatic memory management.

Garbage collection is related to memory management and helps to improve the performance of the application. Garbage collection in a system should not be too quick or too late. Too many GC cycles degrade the performance of the system and cause a spike in CPU whereas a delay in GC cycles leads to memory leakage.

Example:

Let’s consider you are a bachelor and get a call from your mom that she is coming to your flat in the next 3 hours. While talking, you just turn around and see the condition of the flat which is worst than ever think. When the call gets over the first question comes to your mind “Where would I let my mom sit in the room?”. See the left side snap of the room with all the important and garbage things that are messed up and not a single place even to stand.

Garbage Collection Overview
Figure 01: Left – Before GC; Right – After GC

After taking a deep breath, you start picking the things and placing them in their proper place. Then start dusting and brooming the room. Also, trash the garbage in the bin and make the space available to walk and sit. After 3 hours of breakless effort, you made the room clean in which you can comfortably welcome your mom. Refer to the right-side picture of the same room.

This is a real-life example of a Garbage Collector where the room is a heap memory, important households are live/reachable objects which need to be placed in the proper place and garbages are the dead/unreachable objects which need to be thrown out so that room space can be reclaimed, your mom and her luggage are new objects for which you have created some spaces and at last, you were acting like a Garbage Collector.

Type of Garbage Collector:

There are four types of Garbage Collectors:

  1. Serial Garbage Collector
  2. Parallel Garbage Collector
  3. CMS Garbage Collector
  4. G1 Garbage Collector

Garbage Collection method involves:

  1. Mark: To mark the generation of the object. 
  2. Delete/Sweep: To delete the unreachable/dead object.
  3. Compact: To compact the memory by moving around the object.

Some important terms:

  1. Live Object: The object which is referenced by another object
  2. Dead Object: The object which is not referenced by any other object and is unreachable
  3. Daemon Thread: Garbage collection process is carried out by Daemon Thread
  4. System.gc(): It is used to invoke the garbage collector and on invocation, the garbage collector will run to reclaim the unused memory space. The System.gc() is a static method.

You may be interested:


4 thoughts on “Garbage Collection – Overview”

Leave a Comment