Open In App

Java Runtime gc() Method with Examples

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

By recovering space from useless resources, garbage collection in Java streamlines the time-consuming operation of memory management. When memory becomes scarce, the JVM’s garbage collection kicks off immediately. However, developers can utilize the gc() function to actively contact the JVM to perform the work.

  • This method is part of the java.lang.Runtime class if you connect to the Java Virtual Machine.
  • Developers can actively request that the JVM lower its memory use by removing any unneeded resources by using the gc() function.
  • Runtime.getRuntime().gc() or System.gc() can be used to obtain the gc() function from the Runtime class.

Explanation of Concepts

  • Garbage Collection: Garbage collection is the process of detecting and releasing memory that the program is no longer using. The JVM does this automatically in Java, but you may request it directly using the gc() function.
  • Runtime Class: The Runtime class, which is included in the java.lang package provides access to the application’s runtime environment. It enables communication with the underlying system, including interface with the garbage collector.

The gc() function is straightforward, with no variations or overloads. It may be accessed using Runtime.getRuntime().gc() or System.gc().

Example of Java Runtime gc() Method

Here are some example codes demonstrating the use of the gc() method:

Example 1:

In this example,, we will discuss Runtime.getRuntime().gc() to request garbage collection.

Java




//Java Program to 
//create garbage collector 
  
public class GFG {
      
      //Main method
    public static void main(String[] args) {
  
      // print when the program starts
      System.out.println("Program starting...");
  
      // run the garbage collector
      System.out.println("Running Garbage Collector...");
        
      // garbage collector instance
      Runtime.getRuntime().gc();
      System.out.println("Completed.");
    }  
}


Output

Program starting...
Running Garbage Collector...
Completed.





Explanation of the above Program:

  • In this example, we request garbage collection using System.gc() the message is printed to indicate that the garbage collector was requested.

Example 2: Garbage Collection Before Out of Memory

In this example, we will discuss System.gc() to request garbage collection.

Java




// Java Program to create garbage collector 
// using System.gc()
  
// Driver class
public class GFG {
  
    // Main method
    public static void main(String[] args){
        for (int i = 0; i < 100000; i++) {
            // Create objects in a loop
            new GFG();
        }
  
        // Request garbage collection
        System.gc();
  
        // Message to indicate that the garbage collector
        // was requested
        System.out.println("Garbage collection requested.");
    }
}


Output

Garbage collection requested.





Explanation of the Above Program:

  • In this example, we create a large number of GCDemo objects in a loop. Before we run out of memory, we explicitly request garbage collection to free up memory.

Conclusion

The gc() method in Java allows for a targeted release of memory occupied by unused objects. While Java’s built-in garbage collection is generally effective, it may be necessary to address runtime issues.

  • The gc() method serves as a solution.
  • It’s important to use garbage collection with clear justification.
  • The gc() method is a valuable resource for optimizing memory usage and boosting performance in your Java applications.

When utilized thoughtfully, it can be a valuable asset in your programming toolkit.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads