Java System gc() example

In this guide, you will learn about the System gc() method in Java programming and how to use it with an example.

1. System gc() Method Overview

Definition:

The gc() method provides a hint to the Java Virtual Machine (JVM) to initiate garbage collection, aiming to reclaim memory used by discarded objects.

Syntax:

public static void gc()

Parameters:

None.

Key Points:

- The gc() method is essentially a suggestion, not a command, for the JVM to perform garbage collection. The JVM is free to ignore this request.

- Directly invoking garbage collection is typically not recommended because the JVM usually does an efficient job managing memory without explicit prompts.

- There can be performance overheads when invoking garbage collection manually, especially in large-scale applications.

- It can be useful in specific scenarios, such as before a resource-intensive operation where maximum memory availability is crucial.

- Remember, calling System.gc() does not guarantee the garbage collector will run immediately. The JVM decides the right time.

2. System gc() Method Example 1

public class GarbageCollectionExample {
    public static void main(String[] args) {
        // Creating objects
        for (int i = 0; i < 500000; i++) {
            String temp = new String("Object " + i);
        }

        // Requesting JVM to run Garbage Collector
        System.out.println("Requesting Garbage Collection...");
        System.gc();

        // Continuing with the program
        System.out.println("Program continues...");
    }
}

Output:

Requesting Garbage Collection...
Program continues...

Explanation:

In this example, we are creating a large number of String objects in a loop. These objects are local to the loop and become eligible for garbage collection once the loop finishes. By calling System.gc(), we are suggesting to the JVM to run the garbage collector. 

While we see the "Requesting Garbage Collection..." message in the output, there's no direct evidence of garbage collection having occurred, as this is a JVM-managed process. The "Program continues..." message demonstrates that the program executes further after the gc() hint.

3. System gc() Method Example 2

public class GcExample {

    public static void main(String[] args) {

        // Creating objects
        GcExample object1 = new GcExample();
        GcExample object2 = new GcExample();

        // Nullifying the references
        object1 = null;
        object2 = null;

        // Requesting JVM to run Garbage Collector
        System.gc();

        // The finalize method will be called by the Garbage Collector, if overridden
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected: " + this);
    }
}

Output:

Garbage collector called

Explanation:

In this example:

  • We create two objects of the GcExample class.
  • We then nullify the references object1 and object2, making the objects eligible for garbage collection. 
  • We request the JVM to run the Garbage Collector by calling System.gc().
  • We have overridden the finalize() method to print a message to the console when the Garbage Collector is run. 

When you run the program, you might see the output indicating that the Garbage Collector has been called and the objects have been collected, but keep in mind that there is no guarantee this will happen every time.

Comments