Runtime类

Runtime类表示运行时的操作类,是一个封装了JVM进程的类,每一个JVM都对应着一个Runtime类的实例,此实例由JVM运行时为其实例化。

//=================================================
// File Name       :	StringBuffer_demo
//------------------------------------------------------------------------------
// Author          :	Common




//主类
//Function        : 	StringBuffer_demo
public class Runtime_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Runtime run = Runtime.getRuntime();
		
		System.out.println("JVM最大内存:"+run.maxMemory());
		System.out.println("JVM空闲内存量:"+run.freeMemory());
		String str = "HELLO";
		for (int i=0;i<1000;i++){
			System.out.println(str += i);
		}
		System.out.println("JVM空闲内存量:"+run.freeMemory());
		run.gc();					//进行垃圾收集,释放空间
		System.out.println("JVM空闲内存量:"+run.freeMemory());
	}

}

Runtime类与Process类

可以直接使用Runtime类运行本机的可执行程序

//=================================================
// File Name       :	StringBuffer_demo
//------------------------------------------------------------------------------
// Author          :	Common




//主类
//Function        : 	StringBuffer_demo
public class Runtime_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Runtime run = Runtime.getRuntime();
	
		try{
			run.exec("notepad.exe");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

可以通过控制Process进行系统的进程控制,如果想要让进程消失,则可以使用destroy()方法

//=================================================
// File Name       :	StringBuffer_demo
//------------------------------------------------------------------------------
// Author          :	Common




//主类
//Function        : 	StringBuffer_demo
public class Runtime_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Runtime run = Runtime.getRuntime();
	
		Process pro = null;		//声明一个Process对象,接收启动的进程
		
		try{
			pro = run.exec("notepad.exe");
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			Thread.sleep(5000);
		}catch(Exception e){
			e.printStackTrace();
		}
		pro.destroy();
	}

}
原文地址:https://www.cnblogs.com/tonglin0325/p/5263691.html