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(); } } } }
Rumtime类使用
原创
©著作权归作者所有:来自51CTO博客作者xiaohongyangok的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:坦克大战
下一篇:资源文件的使用,实现国际化
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
iOS/Swift Rumtime API
Objective-C RuntimeObjective-C runtime是一个运行库,它提供对Objective-C语言的动态属性的支持,因此所有Objective-C应用程序都链接到它。Objective-C运行库支持函数在位于/usr/lib/libobjc.A.dylib的共享库中实现。在Objective-C中编程时,通常不需要直接使用Objective-C运行时库。此API主要用于开发Objective-C和其他语言之间的桥接层,或用于低级调试。Objective-C运行库的macO.
Runtime API iOS objective-c 运行库 -
使用thread类
package thread;pu
Thread runnable