System:作为系统类,在JDK的java.lang包中,可见它也是一种java的核心语言特性。System类的构造器由private修饰,不允许被实例化。因此,类中的方法也都是static修饰的静态方法。
Runtime: 类代表着Java程序的运行时环境,每个Java程序都有一个Runtime实例,该类会被自动创建,我们可以通过Runtime.getRuntime()
方法来获取当前程序的Runtime实例。
Demo:
public class Test {
public static void main(String[] args) {
// JSONArray json= JSONArray.fromObject("{'code':'3','responseData':'错误'}");
Properties properties = System.getProperties();
for (Map.Entry<Object, Object> entry : properties.entrySet() ) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println("===================================== Runtime 获取系统内存等信息 ===============================================");
Runtime runtime = Runtime.getRuntime();
//获取可用内存
long value = runtime.freeMemory();
System.out.println("可用内存为:"+value/1024/1024+"mb");
//获取jvm的总数量,该值会不断的变化
long totalMemory = runtime.totalMemory();
System.out.println("全部内存为:"+totalMemory/1024/1024+"mb");
//获取jvm 可以最大使用的内存数量,如果没有被限制 返回 Long.MAX_VALUE;
long maxMemory = runtime.maxMemory();
System.out.println("可用最大内存为:"+maxMemory/1024/1024+"mb");
int jvmCore = runtime.availableProcessors();
System.out.println("系统核心数:"+jvmCore);
System.out.println("======================================= 执行系统的命令 =============================================");
try {
Process process = runtime.exec("calc");
} catch (Exception e) {
e.printStackTrace();
}
}
}