Runtime和process
public class test2 { public static void main(String[] args) { Runtime run = Runtime.getRuntime();// 通过Runtime类的静态方法为其进行实例化操作 System.out.println("JVM空闲:" + run.freeMemory()); System.out.println("JVM最大内存" + run.maxMemory()); String str = "hello" + "word" + "!!!" + "\t" + "Welcone" + "to" + "china" + "~"; for (int i = 0; i < 100; i++) { str += i; } System.out.println("操作之后de jVM内存:" + run.freeMemory()); run.gc();// 进行垃圾收集,释放空间 System.out.println("垃圾回收后JVM内存量:" + run.freeMemory()); Process pro = null;// 声明一个对象接受启动的进程 try { pro = run.exec("notepad.exe"); } catch (Exception e) { // TODO: handle exception } try { Thread.sleep(5000); } catch (Exception e) { // TODO: handle exception } pro.destroy();// 结束此进程 } }
结果:
记事本运行五秒后自动关闭