C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Java Garbage Collection with Examples

Submitted by Mayank Singh, on June 09, 2017

Learn: Java’s Garbage Collection, It’s Working and use of finalize() method. This article will explain about the Garbage Collector in Java.

As we know,
Objects in Java are Reference Variables. They are declared to be of specific type and their type can never alter.

While writing a Java Program many Objects are created but not all of the Objects are actually used in the program. Memory Space is allocated to all these Objects but some of them are not used and so the memory space must be made available for the creation of other new Objects.

This is where Garbage Collection comes into play.

When any Object is assigned with null or they lose their Reference, then it becomes the responsibility of Java Runtime Environment (JVM) to mark the location of object as Garbage Collection (GC).

Java Virtual Machines creates three Threads when we run a Java Program.

  1. Main Thread - It is responsible for executing the main method of the java program.
  2. Scheduler Thread - It is responsible for Scheduling the Thread.
  3. Garbage Collector Thread - It is responsible for cleaning the memory heap of the object which was dereferenced by assigning it with null.

Before cleaning the memory of the object assigned with null, it calls finalize() method of that object. After the execution of finalize() method operations under the finalize() method are executed, and the object is destructed.

System.gc();
/* This Method Destroys Location of those Objects which are marked with GC.*/

Note: System.gc() follows Stack based operation , we will see that in the coming example.

Consider the program:

Snippet 1:

import java.util.Scanner;
class IceCreamParlour
{
	String IceCreamName;
	int IceCreamCost;
	Scanner KB=new Scanner(System.in); 

	void getIceCreamDetails()
	{
		System.out.println("Enter Ice Cream Name : ");
		IceCreamName=KB.nextLine();
		System.out.println("Enter Ice Cream Cost : ");
		IceCreamCost=KB.nextInt();
	}

	void putIceCreamDetails()
	{
		System.out.println("Ice Cream Name :"+IceCreamName);
		System.out.println("Ice Cream Cost :"+IceCreamCost);
	}

	protected void finalize()
	{
		System.out.println("Hope ! finishing the "+IceCreamName+" IceCream worth Rs."+IceCreamCost+"/- was FUN ! :D");
	}
}

class IceCreamCollected
{
	public static void main(String args[])
	{
		IceCreamParlour I1=new IceCreamParlour();
		I1.getIceCreamDetails();
		I1.putIceCreamDetails();
		I1=null;
		System.gc();	
	}
}

Output

Enter Ice Cream Name :
Choco
Enter Ice Cream Cost :
50
Ice Cream Name :Choco
Ice Cream Cost :50
Hope ! finishing the Choco IceCream worth Rs.50/- was FUN ! :D

Let’s Change the Snippets Under the class IceCreamCollected

Snippet 2:

class IceCreamCollected
{
	public static void main(String args[])
	{
		IceCreamParlour I1=new IceCreamParlour();
		I1.getIceCreamDetails();
		I1.putIceCreamDetails();
		IceCreamParlour I2=new IceCreamParlour();
		I2.getIceCreamDetails();
		I2.putIceCreamDetails();
		I1=null; // Takes the bottom position in a Stack
		I2=null; // Takes position right above I1
		System.gc();	
	}
}

Output

Enter Ice Cream Name :
Choco
Enter Ice Cream Cost :
50
Ice Cream Name :Choco
Ice Cream Cost :50
Enter Ice Cream Name :
Vanilla
Enter Ice Cream Cost :
100
Ice Cream Name :Vanilla
Ice Cream Cost :100
Hope ! finishing the Vanilla IceCream worth Rs.100/- was FUN ! :D
Hope ! finishing the Choco IceCream worth Rs.50/- was FUN ! :D

Snippet 3:

class IceCreamCollected
{
	public static void main(String args[])
	{
		IceCreamParlour I1=new IceCreamParlour();
		I1.getIceCreamDetails();
		I1.putIceCreamDetails();
		IceCreamParlour I2=I1;
		I1=null;
		System.gc();	
	}
}

Output

Enter Ice Cream Name :
Choco
Enter Ice Cream Cost :
50
Ice Cream Name :Choco
Ice Cream Cost :50

Note: In Snippet 3 , finalize() method was not executed as the Object I1 was having another reference that is of I2.









COMMENTS