Skip to main content

Command Palette

Search for a command to run...

Understanding Java Garbage Collection

Updated
3 min read
Understanding Java Garbage Collection

1. Why Garbage Collection Exists

In many programming languages, developers must manually free memory when objects are no longer needed. If memory is not properly released, the program may suffer from memory leaks or eventually run out of memory.

Java solves this problem with Garbage Collection (GC).

Garbage Collection automatically identifies objects that are no longer used by the program and reclaims their memory so it can be reused.

This allows developers to focus on application logic instead of manual memory management.


2. When an Object Becomes Garbage

An object becomes eligible for garbage collection when it is no longer reachable by any part of the program.

Example

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

public class GCExample {

    public static void main(String[] args) {

        Person p = new Person("EJ");

        p = null;

        System.gc();

        System.out.println("Program finished"); // Output: Program finished
    }
}

Explanation

  1. A Person object is created in the heap

  2. The variable p references that object

  3. When p is set to null, the object becomes unreachable

  4. The garbage collector can reclaim the memory later

Note that calling System.gc() does not guarantee immediate garbage collection. It only suggests that the JVM perform GC.


3. Heap Generations

To improve performance, the JVM divides the heap into different regions called generations.

This design is based on an observation:

Most objects die young.

In other words, many objects are created and discarded quickly.

3.1. Young Generation

The Young Generation is where new objects are initially allocated.

It typically contains:

  • Eden

  • Survivor spaces

Most objects created in applications are short-lived and are collected here.


3.2. Old Generation

Objects that survive multiple garbage collection cycles in the Young Generation are moved to the Old Generation.

This area stores longer-lived objects such as:

  • application-level objects

  • cached data

  • objects referenced for a long time

Because objects in this region tend to live longer, garbage collection here occurs less frequently.


4. Minor GC vs Major GC

Garbage collection can occur in different regions of the heap.

Minor GC Major GC
Occurs in the Young Generation Occurs in the Old Generation
fast slower
frequent less frequent
short pause time longer pause time

5. Stop-The-World

During certain garbage collection phases, the JVM temporarily pauses application threads. This event is known as Stop-The-World (STW).

While the garbage collector runs, normal program execution is paused.

Conceptually

Application Threads → paused
Garbage Collector → running
Application Threads → resumed

Modern JVM implementations attempt to minimize these pauses using optimized garbage collection algorithms.


Conclusion

Garbage Collection is an automatic memory management mechanism in Java that removes objects that are no longer reachable.

Key points

  • Objects become garbage when they are no longer reachable

  • The JVM manages memory automatically through GC

  • The heap is divided into Young Generation and Old Generation

  • Minor GC cleans the Young Generation

  • Major GC cleans the Old Generation

  • Some GC phases cause Stop-The-World pauses

Understanding how garbage collection works helps developers reason about memory usage and application performance.


🔗 References

6 views

Java

Part 3 of 14

Write about basic java preparing for job interviews.

Up next

String vs StringBuilder

Why StringBuilder Exists in Java