In Java, garbage collection is a process of providing Automatic Memory Management by reclaiming the runtime unused memory automatically which destroy unused objects.

How to request JVM For Garbage Collector:

  • System.gc() method is System class which contain static method gc() which request JVM to run Garbage Collector.
  • Runtime.getRuntime().gc() method is a Runtime class which allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.
  • There is no guarantee that any of the above two methods will run Garbage Collector
  • The call System.gc() is effectively equivalent to the call Runtime.getRuntime().gc()

Object be Can unreferenced For Garbage Collector by

1) nulling the reference

Animal a=new Animal ();

2) assigning a reference to another

Animal a1=new Animal();
Animal a2=new Animal();

3) anonymous object etc.

new Animal();

finalize() method

  • The finalize() method is invoked each time before the object is garbage collected to perform cleanup activities. Once finalize() method completes, Garbage Collector destroys that object.
  • finalize() method is present in Object class

protected void finalize() throws Throwable

Based on requirement, one can override finalize() method for performing cleanup activities like closing connection from the database.

  • Although Garbage Collector is one of the modules of JVM, the finalize() method is called by Garbage Collector, not JVM
  • Since object class finalize() method has an empty implementation, the finalize() method needs to be overriden to dispose of system resources or perform other cleanups
  • The finalize() method is never invoked more than once for any object.
  • If an uncaught exception is thrown by the finalize() method, the exception is ignored, and the finalization of that object terminates.

Advantages of Garbage Collector:

  • It makes java memory-efficient by removing unreferenced objects from heap memory.
  • It is automatically done by the garbage collector(a part of JVM)

Examples Of Garbage Collector:

Example 1:

In below example garbage collection is done by calling System.gc().finalize() method is overridden and is invoked just before a object is destroyed by java garbage collection process. Hence you see this method has been invoked twice in the output.

 public class Animal{   
	public static void main(String args[]){  
          Animal animal_obj=new Animal();  

          Animal a1 = new Animal();
          Animal a2 = new Animal();
          a2 = a1;

          System.out.println("Garbage Collector Testing\n");
   protected void finalize() throws Throwable
        System.out.println("Garbage collection is performed by JVM");


Garbage Collector Testing

Garbage collection is performed by JVM
Garbage collection is performed by JVM

Example 2:

In below example to make request to garbage collector, following 3 steps are used before closing brace of sub-block.

  • Set references to null. In this case X = Y = null;
  • Call System.gc();
  • Call System.runFinalization();
class Student {
    private int STUD_ID;
    private String student_name;
    private int student_age;
    private static int nextStudentId = 1;
    public Student(String student_name, int student_age)
        this.student_name = student_name;
        this.student_age = student_age;
        this.STUD_ID = nextStudentId++;
    public void display()
        System.out.println("Student Id=" + STUD_ID + "\nStudent Name=" + student_name + "\nStudent Age=" + student_age + "\n");
    public void showNextStudentId()
        System.out.println("Next student id will be=" + nextStudentId + "\n");
    protected void finalize()

public class DisplayStudent {
    public static void main(String[] args)
        Student A = new Student("Mahesh", 31);
        Student B = new Student("Ramesh", 45);
        Student C = new Student("Suresh", 60);
            Student X = new Student("Hitesh", 15);
            Student Y = new Student("Pritesh", 18);
            X = Y = null;


Student Id=1
Student Name=Mahesh
Student Age=31

Student Id=2
Student Name=Ramesh
Student Age=45

Student Id=3
Student Name=Suresh
Student Age=60

Next student id will be=4

Next student id will be=4

Next student id will be=4

Student Id=4
Student Name=Hitesh
Student Age=15

Student Id=5
Student Name=Pritesh
Student Age=18

Next student id will be=6

Next student id will be=6

Next student id will be=4