package com.xiao;
/*
* Runtime类
* Runtime.getRuntime()获取实例
* freeMemory()获取空闲内存值
* maxMemory()获取最大内存值
* gc()清空内存垃圾
* exec()调用系统程序
*/
public class test_break {
public static void main(String[] args){
test_break ccc = new test_break();
System.out.println("从1加到1000消耗的内存:");
int beforMemory=ccc.freeMemorys();
String test="";
for(int i=1;i<1001;i++){
test+=i;
}
System.out.println("test最后值"+test);
System.out.println("空闲内存"+beforMemory+'\n');
ccc.freeMemory();
ccc.gcMeorys();
ccc.maxMemory();
ccc.notePad();
}
public int test(){
return 3;
}
public void freeMemory(){
Runtime r = Runtime.getRuntime();
System.out.print("空闲内存"+r.freeMemory()+"\n");
}
public int freeMemorys(){
Runtime r = Runtime.getRuntime();
return (int) r.freeMemory();
}
public void gcMeorys(){
Runtime r = Runtime.getRuntime();
r.gc();
System.out.println("清空垃圾后的内存"+r.freeMemory());
}
public void maxMemory(){
Runtime r = Runtime.getRuntime();
r.gc();
System.out.println("JVM最大内存"+r.maxMemory());
}
/*
* 通过Runtime类的exec()方法调用系统程序
*/
public void notePad(){
Runtime r = Runtime.getRuntime();
Process pro=null;
try {
pro=r.exec("notePad.exe");
} catch (Exception e) {
// TODO: handle exception
//e.printStackTrace();
System.out.println("程序不存在");
}
showtime show=new showtime();
Thread t=new Thread(show);
t.start();
try {
Thread.sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
pro.destroy();
}
}
/*
* 输出已逝去的秒数
*/
class showtime implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=5;i++){
System.out.println(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
转载于:https://blog.51cto.com/6846041/1353338