Tag: island of isolation

Garbage Collection in Java | How to make object eligible for Garbage Collection

Garbage Collection in Java-

 

  • It is recommended that if there exist some objects which are not required anymore, programmers make those objects eligible for garbage collection.

 

Thumb rule

An object qualifies for garbage collection if and only if it does not contain any external reference to it.

 

  • So, an object can be made eligible for garbage collection be removing all the external references pointing to it.

 

Ways to make an object eligible for garbage collection in Java-

 

There are 4 different ways in which an object can be made eligible for the purpose of garbage collection-

 

 

  1. By nullifying the reference variable
  2. By reassigning the reference variable
  3. By creating objects inside a method
  4. Island of Isolation

 

1. By nullifying the reference variable-

 

As the name suggests,

In this method, programmer assigns null to the reference variables of all those objects which are no longer required. This makes the useless objects automatically eligible for the purpose of garbage collection.

 

Example-

Consider the following lines of code-

1. Student s1 = new Student( );
2. Student s2 = new Student( );
3.            .
4.            .
5.            .
6.            .
7.         s1 = null;
8.            .
9.            .
10.           .
11.           .
12.        s2 = null;
13.           .
14.           .
15.           .
16.           .

Now, let us perform the analysis of the above lines of code-

Analysis-

 

  • Statement-1 and Statement-2 create two Student objects where reference variable s1 references the first object and reference variable s2 references the second object as shown-

 

 

  • Till statement-6, there exists no object which is eligible for garbage collection.
  • After the execution of statement-7, the first object referenced by s1 becomes eligible for garbage collection as the null reference gets assigned to it as shown-

 

 

  • After the execution of statement-12, the second object referenced by s2 also becomes eligible for garbage collection as the null reference gets assigned to it as shown-

 

 

2. By reassigning the reference variable-

 

As the name suggests,

In this method, programmer reassigns the reference variables of all those objects which are no longer needed to some other objects. This makes the useless objects automatically eligible for the purpose of garbage collection.

 

Example-

Consider the following lines of code-

1. Student s1 = new Student( );
2. Student s2 = new Student( );
3.            .
4.            .
5.            .
6.            .
7.         s1 = new Student( );
8.            .
9.            .
10.           .
11.           .
12.        s2 = s1;
13.           .
14.           .
15.           .
16.           .

Now, let us perform the analysis of the above lines of code-

Analysis-

 

  • Statement-1 and Statement-2 create two Student objects where reference variable s1 references the first object and reference variable s2 references the second object as shown-

 

 

  • Till statement-6, there exists no object which is eligible for garbage collection.
  • Statement-7 creates a new Student object and reassigns the reference variable s1 to this new object. As a result, first object loses its reference and becomes eligible for garbage collection.

 

 

  • Statement-12 makes the second object also eligible for garbage collection as it makes the reference variable s2 point to the object to which the reference variable s1 points.

 

 

3. By creating objects inside a method-

 

As the name suggests,

In this method, programmer creates the objects inside the methods because after the method gets executed completely, objects present inside the method body automatically becomes eligible for the purpose of garbage collection.

The reason behind it is that the reference variables are the local variables of the method and after the method has executed completely, all the local variables are automatically destroyed.

 

Example-

Consider the following lines of code-

1. class Test
2. {
3.    public static void main(String[ ] args)
4.    {
5.        m1( );
6.    }
7.    
8.    public static void m1( )
9.    {
10.       Student s1 = new Student( );
11.       Student s2 = new Student( );
12.   }
13.}

Now, let us perform the analysis of the above lines of code-

Analysis-

 

After the execution of method m1( ) in the main method i.e. after executing statement-5, both the objects becomes eligible for garbage collection and can be collected by the garbage collector.

 

4. Island of Isolation-

 

  • Island of Isolation describes the situation where there exists one or more objects in the program which are not accessible from anywhere in the application as they contain no external reference to them.
  • It is not possible to access such objects through any means as they have no external reference to them and thus they become eligible for garbage collection.
  • So, it appears as if the objects are stuck on some isolated island and they can not be accessed through any means. Hence, the name “Island of Isolation” is quite apt.

 

NOTE-

It is interesting to note that a single object having no reference to it is also an island of isolation.

 

Example-

Consider the following lines of code-

1. class Demo
2. {
3.    Demo d;
4.
5.    public static void main(string[ ] args)
6.    {
7.        Demo d1 = new Demo( );
8.        Demo d2 = new Demo( );
9.        Demo d3 = new Demo( );
10.               .
11.               .
12.               .
13.               .
14.          d1.d = d2;
15.          d2.d = d3;
16.          d3.d = d1;
17.               .
18.               .
19.               .
20.               .
21.            d1 = null;
22.            d2 = null;
23.            d3 = null;
24.   }
25. }

Now, let us perform the analysis of the above lines of code-

Analysis-

 

  • Statement-7, statement-8 and statement-9 create three Student objects which are referenced by the reference variables d1, d2 and d3 respectively as shown-

 

 

Till now, each object is having exactly one reference variable and no object is eligible for garbage collection.

  • Statement-14 assigns a copy of d2 to d1.d. So, now d1.d points to the object of d2.
  • Statement-15 assigns a copy of d3 to d2.d. So, now d2.d points to the object of d3.
  • Statement-16 assigns a copy of d1 to d3.d. So, now d3.d points to the object of d1.

 

 

Till now, each object is having exactly two reference variables and no object is eligible for garbage collection.

 

  • Statement-21 assigns null to the reference variable d1. Thus, now d1 references to no one. Still, no object is eligible for garbage collection.
  • Statement-22 assigns null to the reference variable d2. Thus, now d2 references to no one. Still, no object is eligible for garbage collection.
  • Statement-23 assigns null to the reference variable d3. Thus, now d3 references to no one.

 

Now, all the three objects become eligible for garbage collection as now there does not exist even one external reference to them.

 

An isolated island is created where all the three objects are stuck and there is no communication with any active part of the application.

All the three objects internally references each other but there is no external reference which makes the three objects eligible for garbage collection.

 

Island of Isolation

 

Get more notes and other study material of Core Java.

Watch video lectures by visiting our YouTube channel LearnVidFun.