Java System gc() Method
Example
Ask Java to free the memory of an array that is no longer used:
public class Main {
public static void main(String[] args) {
int[] myNumbers = new int[1000000];
myNumbers = null;
System.gc();
System.out.println("The array can now be removed from memory");
}
}
Definition and Usage
The gc() method asks the Java Virtual Machine (JVM, the program that runs Java code) to run the garbage collector. The garbage collector frees the memory of objects that the program can no longer use.
It is only a suggestion. Java decides itself when to clean up memory, so you rarely need to call this method.
Syntax
public static void gc()
Parameter Values
None.
Related Pages
❮ System Methods