java获取系统信息(CPU,内存,硬盘,进程)的相关方法
一、这是一个获取相关信息的简单的方法
import java.io.*;
import com.sun.management.OperatingSystemMXBean;
import sun.management.ManagementFactory;
public class Tst{
public static String pt="D:\\";
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 Runtime();
alMemory();
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) OperatingSystemMXBean();
long FreePhysicalMemorySize()/kb;
long TotalPhysicalMemorySize()/kb;
long physicalUse=physicalTotal-physicalFree;
String Property("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 = Parent())
;
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;
get out ofimport java.util.List;
import java.io.LineNumberReader;
import java.util.ArrayList;
get out ofimport java.util.List;
import mytools.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/
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 CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final int FAULTLENGTH = 10;
public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}
//获取内存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) OperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = TotalSwapSpaceSize();
// 剩余的物理内存
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) OperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = TotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = FreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}
//获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
ists()){
long total=(TotalSpace();
long free=(FreeSpace();
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
ists()){
long total=(TotalSpace();
long free=(FreeSpace();
Double compare=(Double)(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论