每一个 JVM 进程都会存在一个 Runtime 类对象,类的功能是取得一些与运行时有关的环境属性,或者创建新的进程等操作。
Runtime 类属于单例设计模式,构造方法被私有化了,为了保证整个进程中只有一个 Runtime 类对象。
Runtime 类是所有与本地运行有关的所有相关属性的集合。
GC(Garbage Collector):垃圾收集器,释放无用的内存空间。由系统不定期的自动进行回收,或者调用 Runtime 类中的 gc 方法手动回收。
public class Main {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.maxMemory()); // 最大可用内存空间
System.out.println(runtime.totalMemory()); // 所有可用内存空间
System.out.println(runtime.freeMemory()); // 空余内存空间
runtime.gc(); // 释放垃圾空间。
}
}