java.lang.Runtime类总结 【转】

简介: 转自:http://blog.chinaunix.net/uid-128922-id-289994.html  Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够 与其运行的环境相连接。

转自:http://blog.chinaunix.net/uid-128922-id-289994.html

 Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够
与其运行的环境相连接。一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类
实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用。 一旦得到了一个当前的
Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为。
常见的应用有 1:执行外部程序(调用外部命令) 举例:用java调用外部mcl数据分析工具(之前我已经把mcl安装的bin目录导入到了PATH中) String [] cmd={"cmd","/C","mcl D:\\input.txt --abc -o D:\\output.txt -I 1 -scheme 7"}; Process proc =Runtime.getRuntime().exec(cmd);
注意目录中用“\\”代替"\"。 如果外部命令过于复杂可以自己写一个bat或shell脚本然后调用 如下 String [] cmd={"cmd","/C","E:\\test3.bat"}; Process proc =Runtime.getRuntime().exec(cmd);
下面对Runtime.exec()做个总结

Windows下调用程序

Process proc =Runtime.getRuntime().exec("exefile");

Linux下调用程序就要改成下面的格式

Process proc =Runtime.getRuntime().exec("./exefile");

Windows下调用系统命令

String [] cmd={"cmd","/C","copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Linux下调用系统命令就要改成下面的格式

String [] cmd={"/bin/sh","-c","ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Windows下调用系统命令并弹出命令行窗口

String [] cmd={"cmd","/C","start copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Linux下调用系统命令并弹出终端窗口就要改成下面的格式

String [] cmd={"/bin/sh","-c","xterm -e ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

还有要设置调用程序的工作目录就要

Process proc =Runtime.getRuntime().exec("exeflie",null, new File("workpath"));
2:内存管理
Java提供了无用单元自动收集机制。通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少。
Java会周期性的回收垃圾对象(未使用的对象),以便释放内存空间。但是如果想先于收集器的下一次指定周期来收集废弃的对象,可以通过调用gc()方法来根据需要运行无用单元收集器。
一个很好的试验方法是先调用gc()方法,然后调用freeMemory()方法来查看基本的内存使用情况,接着执行代码,然后再次调用freeMemory()方法看看分配了多少内存。
下面的程序演示了这个构想。
class MemoryDemo{ 
         public  static  void main(String args[]){ 
                Runtime r = Runtime.getRuntime(); 
                 long mem1,mem2; 
                Integer someints[] =  new Integer[1000]; 
                System.out.println( "Total memory is :" + r.totalMemory()); 
                mem1 = r.freeMemory(); 
                System.out.println( "Initial free is : " + mem1); 
                r.gc(); 
                mem1 = r.freeMemory(); 
                System.out.println( "Free memory after garbage collection : " + mem1); 
                 //allocate integers 
                 for( int i=0; i<1000; i++) someints[i] =  new Integer(i);    
                mem2 = r.freeMemory(); 
                System.out.println( "Free memory after allocation : " + mem2); 
                System.out.println( "Memory used by allocation : " +(mem1-mem2));    
                 //discard Intergers 
                 for( int i=0; i<1000; i++) someints[i] =  null
                r.gc();  //request garbage collection 
                mem2 = r.freeMemory(); 
                System.out.println( "Free memory after collecting " +  "discarded integers : " + mem2); 
        } 
}
编译后运行结果如下(不同的机器不同时间运行的结果也不一定一样):
Total memory is :2031616
Initial free is : 1818488
Free memory after garbage collection : 1888808
Free memory after allocation : 1872224
Memory used by allocation : 16584
Free memory after collecting discarded integers : 1888808
 
【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
相关文章
|
7月前
|
Java Spring
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
34 0
|
7月前
|
Java Spring 容器
解决java.lang.StackOverflowError at java.base/java.lang.Double.doubleToRawLongBits(Native Method)问题~
解决java.lang.StackOverflowError at java.base/java.lang.Double.doubleToRawLongBits(Native Method)问题~
没有加载jawt导致java.lang.UnsatisfiedLinkError
没有加载jawt导致java.lang.UnsatisfiedLinkError
102 0
|
安全 Java API
【小家java】聊聊Java中的Runtime类
【小家java】聊聊Java中的Runtime类
|
Java Linux API
【小家java】聊聊Java中的System类
【小家java】聊聊Java中的System类
【小家java】聊聊Java中的System类
|
Java JavaScript 应用服务中间件