The java.lang.System.gc() method runs the garbage collector. Calling this suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.
Following is the declaration for java.lang.System.gc() method
public static void gc()
NA
This method does not return any value.
NA
The following example shows the usage of java.lang.System.gc() method.
package com.tutorialspoint; import java.lang.*; public class SystemDemo { public static void main(String[] args) { int arr1[] = { 0, 1, 2, 3, 4, 5 }; int arr2[] = { 0, 10, 20, 30, 40, 50 }; // copies an array from the specified source array System.arraycopy(arr1, 0, arr2, 0, 1); System.out.print("array2 = "); System.out.print(arr2[0] + " "); System.out.println(arr2[1] + " "); // it runs the GarbageCollector System.gc(); System.out.println("Cleanup completed..."); } }
Let us compile and run the above program, this will produce the following result −
array2 = 0 10 Cleanup completed...