static void gc()
描述 (Description)
java.lang.System.gc()方法运行垃圾收集器。 调用此表明Java虚拟机花费了大量精力来回收未使用的对象,以使其当前占用的内存可用于快速重用。
声明 (Declaration)
以下是java.lang.System.gc()方法的声明
public static void gc()
参数 (Parameters)
NA
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.System.gc()方法的用法。
package com.iowiki;
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...