Java.lang.System.gc() 方法
描述
java.lang.System.gc() 方法运行垃圾收集器。 调用它表明 Java 虚拟机花费精力回收未使用的对象,以使它们当前占用的内存可用于快速重用。
声明
以下是 java.lang.System.gc() 方法的声明。
public static void gc()
参数
NA
返回值
此方法不返回任何值。
异常
NA
示例
下面的例子展示了 java.lang.System.gc() 方法的使用。
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...");
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
array2 = 0 10 Cleanup completed...

