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);
Process proc =Runtime.getRuntime().exec(cmd);
Linux下调用系统命令就要改成下面的格式
String [] cmd={"/bin/sh","-c","ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);
Process proc =Runtime.getRuntime().exec(cmd);
Windows下调用系统命令并弹出命令行窗口
String [] cmd={"cmd","/C","start copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);
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(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);
}
}
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
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
2015-09-18 Linux内核驱动之GPIO子系统(一)GPIO的使用【转】
2015-09-18 Linux内核驱动基础(一)常用宏定义【转】
2015-09-18 Linux设备驱动之Ioctl控制【转】