java Runtime類
阿新 • • 發佈:2018-12-25
public class Test { public static void main(String[] args) throws UnsupportedEncodingException { Runtime r = Runtime.getRuntime();//獲取Runtime例項 p(r.freeMemory());//剩餘記憶體 p(r.totalMemory()/1024/1024);//總記憶體 p(r.maxMemory()/1024/1024);//最大可用記憶體 p(r.availableProcessors());//核心數 List list = new ArrayList(); for(int i=0;i<100000;i++) { list.add("aaaaaaaaaaaaaaaaaaaabbbbbbbbbbb"); } p(r.freeMemory());//剩餘記憶體 r.gc();//垃圾回收, 測試用 實際應用中不要顯式呼叫!! p(r.freeMemory());//剩餘記憶體 String s = ""; try { //執行命令不支援cd切換目錄,可以把目錄作為引數傳進去 Process proc = r.exec("cmd.exe /c dir", null, new File("c:")); //執行有回顯的命令 proc = r.exec("cmd.exe /c dir"); //開啟可執行檔案 r.exec("D:\\Program Files\\cradio_chs\\CRadio.exe"); //執行指令碼 proc = r.exec("cmd.exe /c C:\\Users\\Administrator\\Desktop\\哨兵啟動.cmd"); //獲取回顯 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gbk")); while((s=br.readLine())!=null) { p(s); } //開啟可執行檔案 r.exec("C:\\Program Files\\Microsoft Office\\Office16\\WINWORD.EXE C:\\Users\\Administrator\\Desktop\\違章push規則變更.doc"); r.exec("C:\\Program Files\\Microsoft Office\\Office16\\WINWORD.EXE C:\\Users\\Administrator\\Desktop\\違章\" \"push規則.docx");//空格用 \" \"代替 r.exec("notepad.exe"); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } } public static void p(Object o) { System.out.println(o); } }