分类: Java
2012-12-19 10:10:31
一、这是一个获取相关信息的简单的方法
import java.io.*;
import com.sun.management.OperatingSystemMXBean;
import sun.management.ManagementFactory;
public class Tst{
public static String pt="D:\\abc.txt";
public Tst(){
}
public static void main(String[] args) throws Exception{
//free和use和total均为KB
long free=0;
long use=0;
long total=0;
int kb=1024;
Runtime rt=Runtime.getRuntime();
total=rt.totalMemory();
free=rt.freeMemory();
use=total-free;
System.out.println("系统内存已用的空间为:"+use/kb+" MB");
System.out.println("系统内存的空闲空间为:"+free/kb+" MB");
System.out.println("系统总内存空间为:"+total/kb+" MB");
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long physicalFree=osmxb.getFreePhysicalMemorySize()/kb;
long physicalTotal=osmxb.getTotalPhysicalMemorySize()/kb;
long physicalUse=physicalTotal-physicalFree;
String os=System.getProperty("os.name");
System.out.println("操作系统的版本:"+os);
System.out.println("系统物理内存已用的空间为:"+physicalFree+" MB");
System.out.println("系统物理内存的空闲空间为:"+physicalUse+" MB");
System.out.println("总物理内存:"+physicalTotal+" MB");
// 获得线程总数
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
int totalThread = parentThread.activeCount();
System.out.println("获得线程总数:"+totalThread);
}
}
二,别的方法技巧
1.利用jdk自带的API获取信息:(只支持jdk1.60以上的版本啊)
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import
mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/
public class WindowsInfoUtil
{
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public
static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}
参考