用ManagementFactory获取系统运行信息

import java.lang.management.MemoryUsage;  
import java.util.Date;  
import java.util.List;  
import java.util.Map;  
  
import org.apache.commons.lang.builder.ToStringBuilder;  
  
/** 
 * @author lisen 
 * @date 2013-11-22 上午09:23:30 
 */  
public class SystemInfoBean {  
    // 加载类的数量  
    private int loadClazzCount;  
    // 已经加载类的数量  
    private long hasloadClazzCount;  
    // 尚未加载类的数量  
    private long hasUnloadClazzCount;  
    // 堆内存信息  
    private MemoryUsage heapMemoryUsage;  
    // 非堆内存信息  
    private MemoryUsage nonHeapMemoryUsage;  
    // 操作系统的名称  
    private String operateName;  
    // 操作系统的进程数  
    private int processListCount;  
    // 操作系统的架构  
    private String archName;  
    // 操作系统的版本号码  
    private String versionName;  
    // 虚拟机的名称  
    private String vmName;  
    // 虚拟机的版本  
    private String vmVersion;  
    // 系统的供应商的名称  
    private String vmVendor;  
    // JVM启动时间  
    private Date startTime;  
    // 输入参数  
    private List<String> arguments;  
    // 系统参数  
    private Map<String, String> systemProperties;  
      
    //setter and getter...  
}

获取信息

import java.lang.management.ClassLoadingMXBean;  
import java.lang.management.ManagementFactory;  
import java.lang.management.MemoryMXBean;  
import java.lang.management.OperatingSystemMXBean;  
import java.lang.management.RuntimeMXBean;  
import java.util.Date;  
  
/** 
 * @author lisen 
 * @date 2013-11-22 上午09:27:03 
 */  
public class SystemInfoUtils {  
  
    private SystemInfoBean infoBean = null;  
  
    private static class SingletonClassInstance {  
        private static final SystemInfoUtils instance = new SystemInfoUtils();  
    }  
  
    public static SystemInfoUtils getInstance() {  
        return SingletonClassInstance.instance;  
    }  
  
    private SystemInfoUtils() {  
        infoBean = new SystemInfoBean();  
  
        // 操作系统信息  
        OperatingSystemMXBean operateSystemMBean = ManagementFactory  
                .getOperatingSystemMXBean();  
        String operateName = operateSystemMBean.getName();  
        infoBean.setOperateName(operateName);  
        int processListCount = operateSystemMBean.getAvailableProcessors();  
        infoBean.setProcessListCount(processListCount);  
        String archName = operateSystemMBean.getArch();// System.getProperty("os.arch");  
        infoBean.setArchName(archName);  
        String versionName = operateSystemMBean.getVersion();// System.getProperty("os.version");  
        infoBean.setVersionName(versionName);  
  
        // 运行时信息  
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();  
        String vmName = runtimeMXBean.getVmName();  
        infoBean.setVmName(vmName);  
        String vmVersion = runtimeMXBean.getVmVersion();  
        // infoBean.setVmVersion(vmVersion);  
        infoBean.setVmVersion(System.getProperty("java.version") + " ("  
                + vmVersion + ")");  
        String vmVendor = runtimeMXBean.getVmVendor();  
        infoBean.setVmVendor(vmVendor);  
        long startTime = runtimeMXBean.getStartTime();  
        infoBean.setStartTime(new Date(startTime));  
  
        infoBean.setArguments(runtimeMXBean.getInputArguments());  
        infoBean.setSystemProperties(runtimeMXBean.getSystemProperties());  
    }  
  
    public SystemInfoBean getSystemInfo() {  
        // 类信息  
        ClassLoadingMXBean classLoadMXBean = ManagementFactory  
                .getClassLoadingMXBean();  
        int loadClazzCount = classLoadMXBean.getLoadedClassCount();  
        infoBean.setLoadClazzCount(loadClazzCount);  
        long hasloadClazzCount = classLoadMXBean.getTotalLoadedClassCount();  
        infoBean.setHasloadClazzCount(hasloadClazzCount);  
        long hasUnloadClazzCount = classLoadMXBean.getUnloadedClassCount();  
        infoBean.setHasUnloadClazzCount(hasUnloadClazzCount);  
  
        // 内存  
        MemoryMXBean memoryUsage = ManagementFactory.getMemoryMXBean();  
        infoBean.setHeapMemoryUsage(memoryUsage.getHeapMemoryUsage());  
        infoBean.setNonHeapMemoryUsage(memoryUsage.getNonHeapMemoryUsage());  
        return infoBean;  
    }  
}
//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);
System.out.println("activeCount:" + Thread.activeCount());



你可能感兴趣的:(用ManagementFactory获取系统运行信息)