Garbage Collection – Overview

When developers write Java programs, their code often creates many objects. These objects are stored in a special area of memory called the heap. But what happens to the memory when we no longer need some of those objects?

That’s where Garbage Collection comes into play.

What is a Garbage Collector?

A Garbage Collector is a Java program that tracks the referenced (live) objects and allows them to stay 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 is the memory given back to the operating system. As long as an object is referenced, the JVM considers it alive and keeps it in 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.

A Real-Life Analogy:

Imagine you’re living alone in your apartment. One day, your mom calls and says she’ll be visiting in three hours. You look around and realize your room is a mess—things are scattered everywhere, and there’s no place for her to sit.

Now you spring into action:

  • You start sorting out what’s useful and what’s trash.
  • You put useful things in their proper place (like live objects).
  • You throw out the garbage and unwanted stuff (like dead objects).
  • Finally, your room is clean and has enough space for your mom and her luggage (new objects).

In this example:

  • The room = Heap Memory
  • Useful items = Live/Referenced Objects
  • Garbage/Trash = Dead/Unreferenced Objects
  • You = The Garbage Collector
  • Your Mom and her luggage = New Objects needing space
Garbage Collection Overview
Figure 01: Left – Before GC; Right – After GC

How does Garbage Collection work?

  • As long as an object is referenced in the java code (e.g., stored in a variable or used somewhere), the JVM keeps it in memory.
  • If no part of the program refers to an object anymore, it is considered unreachable, and the Garbage Collector will eventually remove it from memory.
  • This frees up space for new objects and keeps the application from running out of memory.

Technically, Garbage Collection generally happens in three steps:

  1. Mark
    • The JVM scans and marks all live (reachable) objects.
  2. Delete/Sweep
    • It removes the dead (unreachable) objects from memory.
  3. Compact
    • The memory is then reorganized by moving live objects together, so free space is continuous and easy to allocate.

What Garbage Collection doesn’t do:

  • It doesn’t explicitly delete memory like you might do in C or C++ using free() or delete.
  • It also doesn’t return memory to the operating system. It simply reuses it within the JVM.

Why is Garbage Collection Important?

Garbage collection helps the java application run more efficiently by:

  • Freeing up unused memory so it can be used again.
  • Preventing memory leaks, which happen when unused objects are not removed.
  • Improving performance, but only if used wisely.

However, it’s important to balance the timing:

  • If Garbage Collection runs too often, it can slow down the application because it takes up CPU resources.
  • If it runs too late, the program might run out of memory or experience memory leaks.

Type of Garbage Collector in Java:

There are four types of Garbage Collectors:

  1. Serial Garbage Collector
  2. Parallel Garbage Collector
  3. CMS Garbage Collector
  4. G1 Garbage Collector
  5. Z Garbage Collector (ZGC)
  6. Shenandoah Garbage Collector
Garbage CollectorMain FeatureBest For
Serial GCSimple, single-threadedSmall applications
Parallel GCMulti-threaded, high throughputCPU-rich apps
CMS GCLow pause time, concurrentInteractive apps (deprecated after Java 14)
G1 GCRegion-based, balanced performanceGeneral-purpose use
ZGCUltra-low pause, scalableLarge heap, low-latency apps
Shenandoah GCLow pause with concurrent compactionReal-time systems with large heaps

Some important terms:

  1. Live Object: The object that is referenced by another object
  2. Dead Object: The object that is not referenced by any other object and is unreachable
  3. Daemon Thread: A special background thread that performs garbage collection.
  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.
System.gc(); // Suggests JVM to run the garbage collector

Conclusion

Garbage Collection in Java plays a vital role in automatic memory management, which helps ensure applications run smoothly without running out of memory or crashing due to memory leaks.

As a Performance tester, understanding how garbage collection works allows you to:

  • Identify and report performance issues caused by excessive or delayed garbage collection.
  • Recognize signs of memory leaks during long-running tests or load testing.
  • Design better test scenarios that simulate memory pressure and monitor system behavior.
  • Use tools (like profilers or JVM logs) more effectively to trace memory usage and garbage collection patterns.

You don’t need to write or manage garbage collection code, but having this knowledge helps you test more intelligently, especially when validating application stability, scalability, and performance.


You may be interested:


4 thoughts on “Garbage Collection – Overview”

Comments are closed.