Runtime - 运行环境类
1.基本用法
public class Test01 {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
System.out.println("获取最大内存数(字节):" + run.maxMemory());
System.out.println("获取闲置内存数(字节):" + run.freeMemory());
System.out.println("获取处理数:" + run.availableProcessors());
}
}
2.测试程序效率(时间、内存)
public class Test02 {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
long startTime = System.currentTimeMillis();
long startMemory = run.freeMemory();
StringBuilder sb = new StringBuilder("林成");
for (int i = 0; i < 50000; i++) {
sb.append("小可爱,皇冠给你带");
}
long endMemory = run.freeMemory();
long endTime = System.currentTimeMillis();
System.out.println("消耗时长:" + (endTime-startTime));
System.out.println("消耗内存:" + (startMemory-endMemory));
}
}