How do objects become eligible for garbage collection ?

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

When a program is running, then myriad number of objects are getting created, Strings, Integers, ArrayLists, Maps,even user defined objects are created and are stored in the memory at runtime, but they may cause memory issue. When the memory is being full, then JVM must remove some of the objects from memory in order to create new objects. But how does it decide which objects should be removed from memory. Something called as garbage collection mechanism takes care of this. Let us note down some of the points regarding garbage collection.

  • The purpose of garbage collection is to remove the objects those can’t be reached
  • Objects can be eligible for garbage collection even if they refer to each other

We will first see what exactly these statements mean,

If an object cannot be reached, that means any live thread is not able to access it through any reference variable that is used in a program. Let us see the definition below


  • Object is the Type of the reference variable obj.
  • A reference variable is an identifier that can refer to an object.
  • new Object(); will actually create a new object in the memory.
  • The reference variable obj is now referring to the object

When we do obj = null;, then the assignment of obj to object is removed and the object does not have any reference from our program. This means it is eligible for garbage collection now.

But what happens when an object contains reference to another object? Consider a scenario for three objects, a Room, a Door and a DoorKnob.

  • A Room HAS-A Door
  • A Door HAS-A DoorKnob



We have these three classes. A room has a reference to a door and the door has a reference to a doorknob.

When the line Door door = new Door(); is executed, a new Door object will be created and the variable door will refer to it. As Door HAS-A DoorKnob so as soon as Door object is created, a new DoorKnob will also be created and it will be referred by the reference variable in Door class.

Keep these symbols in mind


Door Object Created

Door Object Created

When the line Room room = new Room();is executed, the scenario is as follows

After creating Room object

After creating Room object

Till this point, its pretty straight forward, now when we are setting the newly created door to the room by room.setDoor(door);, the default door object which was referred by room.door is lost and it does not have any reference from our program now because the only way to refer to it was through room.door.


This could be confirmed if we write the main method as follows

This will output

Don’t worry about the numbers, they print the hashcode of the object, different hashcodes mean different objects.

Now we will remove the reference of room to object by assigning it to null and then see what happens,

What will this output? obviously NullPointerException because room is null now so does that mean the object referred by room is eligible for garbage collection now? Think again, we can access the same object using reference variable door.

THis will print the same hashcode that room.getDoor() previously before room became null.
That means its not eligible for garbage collection yet as it has a link through program.


Lets review it to the point, now giving numbers to the objects

  • Door door = new Door()  creates 1 and and door refers to 1.
  • Room room = new Room  created 3, 4 and 5 and room refers to 3.
  • room.setDoor(door) causes the 3 to lose link to 4, but 4 still has link to 5
  • room = null  causes the variable room to lose link to 3 but 3 still has link to 1.
  • We can’t access 1 through as we can’t access 3.
  • We have direct access to 1 and  access to 2 through 1.

Now how many objects will be eligible for garbage collection? The answer is three objects and those are 3,4 and 5 as they don’t have any links through program reference variables

Hope these diagrams helped understand the concept of garbage collection eligibility.



Share Button

4 comments for “How do objects become eligible for garbage collection ?

  1. Anonymous
    January 25, 2015 at 6:55 pm

    really helpful .Thanks..

  2. Utkarsh
    February 13, 2015 at 1:53 am

    Thanks. Explained very neatly.

  3. ThanhTrinh
    March 11, 2015 at 9:20 am

    Really helpful. Thank you so much !

    • March 11, 2015 at 9:54 am

      I’m glad you found it useful. Happy learning :)

Leave a Reply

Your email address will not be published. Required fields are marked *