DEV Community

Sundar Joseph
Sundar Joseph

Posted on

Types of Activities in Java Garbage Collection

Java heap is divided into generations:

Young Generation: In this new objects are allocated.
Old Generation: In this long-lived objects are stored.
Two types of garbage collection activities usually happen in Java. These are:

Minor or incremental Garbage Collection (GC): This occurs when unreachable objects in the Young Generation heap memory are removed.
Major or Full Garbage Collection (GC): This happens when objects that survived minor garbage collection are removed from the Old Generation heap memory. It occurs less frequently than minor garbage collection.

Key Concepts on Garbage

Collection

  1. Unreachable Objects An object becomes unreachable if it does not contain any reference to it.

Note: Objects which are part of the island of isolation are also unreachable.

Integer i = new Integer(4);

// the new Integer object is reachable via the reference in 'i'

i = null;

// the Integer object is no longer reachable.

Unreachable Objects

2. Making Objects Eligible for GC

collection if it is unreachable. After i = null, integer object 4 in the heap area is suitable for garbage collection in the above image.

How to Make an Object Eligible for Garbage Collection?
Even though the programmer is not responsible for destroying useless objects but it is highly recommended to make an object unreachable(thus eligible for GC) if it is no longer required. There are generally four ways to make an object eligible for garbage collection.

Nullifying the reference variable (obj = null).
Re-assigning the reference variable (obj = new Object()).
An object created inside the method (eligible after method execution).
Island of Isolation (Objects that are isolated and not referenced by any reachable objects).

Top comments (0)