🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # 简介 在每一个JVM进程里面都会存在一个Runtime类的对象, 这个类的主要功能是取得一些与运行时有关的环境属性或者创建新的进程等操作 在Runtime类定义的时候它的构造方法已经被私有化了,这就属于单例设计模式,只有一个Runtime类对象. 所以在Runtime类里面提供一个static型的方法,实例化对象: `public static Runtime getRuntime();` Runtime类是直接与本地运行有关的所有相关属性的集合,所以定义了以下的方法: # 方法 ~~~ public long totalMemory(); //返回所有可用内存 public long maxMemory(); //返回最大可用内存空间 public long freeMemory(); //返回空余内存空间 public native void gc(); //释放垃圾 ~~~ 他可以调用本机可执行程序,可以创建进程 ~~~ public Process exec(String command) throws IOException ~~~ 案例 ~~~ Runtime runtime = Runtime.getRuntime(); System.out.println("MAX: " + runtime.maxMemory()); System.out.println("free: " + runtime.freeMemory()); System.out.println("total: " + runtime.totalMemory()); ~~~ ~~~ public static void main(String[] args) throws IOException, InterruptedException { Runtime runtime = Runtime.getRuntime(); //调用画图程序 Process pro = runtime.exec("mspaint.exe"); Thread.sleep(2000); //销毁进程 pro.destroy(); } ~~~