How to Use System.gc() for Garbage Collection in Java

Sheeraz Gul Feb 02, 2024
How to Use System.gc() for Garbage Collection in Java

System.gc() is an API provided in Java for garbage collection, which performs automatic memory management.

When we run a Java program, there might be some objects or data which is not needed anymore, System.gc() collects that data and deletes them to free up the memory.

This tutorial will demonstrate how to use the System.gc() in Java.

Demonstrate the Use of System.gc() in Java

The System.gc() can be invoked by the system, developer, or the external tools used for the applications.

Example:

package delftstack;
public class System_Gc {
  public static void main(String... args) {
    for (int x = 1; x < 15; x++) {
      Demo_Class New_Demo_Class = new Demo_Class(x);
      System.out.printf("Demo Class Generated, Demo= %s%n", New_Demo_Class.get_Demo());
      System.gc();
    }
  }

  public static class Demo_Class {
    private final int Demo;

    public Demo_Class(int Demo) {
      this.Demo = Demo;
    }

    public int get_Demo() {
      return Demo;
    }

    // the garbage collector will call this method each time before removing the object from memory.
    @Override
    protected void finalize() throws Throwable {
      System.out.printf("-- %s is getting collected in the garbage --%n", Demo);
    }
  }
}

The code above gives the sample use of System.gc(), where a method is called 14 times, and the system collects the garbage 13 times.

Output:

Demo Class Generated, Demo= 1
Demo Class Generated, Demo= 2
Demo Class Generated, Demo= 3
-- 1 is getting collected in the garbage --
Demo Class Generated, Demo= 4
-- 2 is getting collected in the garbage --
Demo Class Generated, Demo= 5
-- 3 is getting collected in the garbage --
Demo Class Generated, Demo= 6
-- 4 is getting collected in the garbage --
Demo Class Generated, Demo= 7
-- 5 is getting collected in the garbage --
Demo Class Generated, Demo= 8
-- 6 is getting collected in the garbage --
Demo Class Generated, Demo= 9
-- 7 is getting collected in the garbage --
Demo Class Generated, Demo= 10
-- 8 is getting collected in the garbage --
-- 9 is getting collected in the garbage --
Demo Class Generated, Demo= 11
Demo Class Generated, Demo= 12
-- 10 is getting collected in the garbage --
-- 11 is getting collected in the garbage --
Demo Class Generated, Demo= 13
-- 12 is getting collected in the garbage --
Demo Class Generated, Demo= 14
-- 13 is getting collected in the garbage --

The System.gc() is not mainly used by developers because they think it has no use, but it is a good tool for handling the cache memory.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Function