准备构建一个日志系统,记录创建的虚拟机的实时性能参数。首先先要获取系统的性能参数,再将参数发送给服务器端存储下来。
首先是搜了一个内存参数的代码(跨平台可用),略微修改,用的jre6:
import java.io.*;
import com.sun.management.OperatingSystemMXBean;
import java.lang.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;
lo