zoukankan      html  css  js  c++  java
  • java.lang.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
     
  • 相关阅读:
    禁止Crontab产生邮件
    centos6.2 ftp 配置
    [转] 夏天折T恤的方法
    [转] 55个经典开源Windows工具(2)
    [转] 55个经典开源Windows工具(3)
    [**轻松一下**] 程序员喝酒喝出的计算机文化!
    [转] 55个经典开源Windows工具(1)
    [转] 关于mscomm的用法,提高篇
    [转] 传说中的减肥............
    [转] 关于MSCOMM控件的一些说明
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/5882398.html
Copyright © 2011-2022 走看看