3.2.1 概述
3.2.2 常用方法
public static Runtime getRuntime() {}
返回 Java 虚拟机中的内存总量(此方法返回的值可能随时间的推移而变化,这取决于主机环境):
public native long totalMemory();
返回 Java 虚拟机中的空闲内存量(调用 gc 方法可能导致 freeMemory 返回值的增加):
public native long freeMemory();
示例:
package com.github.runtime.demo;
/**
* @author maxiaoke.com
* @version 1.0
*/
public class Test {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
Runtime runtime1 = Runtime.getRuntime();
System.out.println(runtime == runtime1); // true
long totalMemory = runtime.totalMemory();
System.out.println("totalMemory = " + totalMemory); // 512753664
long freeMemory = runtime.freeMemory();
System.out.println("freeMemory = " + freeMemory); // 504700544
}
}