java怎样获取CPU占用率和硬盘占用率

通过jmx可以监控vm内存使用,系统内存使用等,以下是网上某博客代码,特点是通过window和linux命令获得CPU使用率。 

 利用java程序实现获取计算机cpu利用率和内存使用信息。 

 

 

 

 

package com.amgkaka.performance;   

   
/** *//**  
 * 监视信息的JavaBean类.  
 * @author  amg  
 * @version 1.0   
 * Creation date: 2008-4-25 - 上午10:37:00  
 */  
public class MonitorInfoBean {   
    /** *//** 可使用内存. */  
    private long totalMemory;   
        
    /** *//** 剩余内存. */  
    private long freeMemory;   
        
    /** *//** 最大可使用内存. */  
    private long maxMemory;   
        
    /** *//** <a href="https://www.baidu.com/s?wd=%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYmhN-mHIWuWPhrHDYnyR40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHczPWTsPjn1rHDkPWTsrjfd" target="_blank" class="baidu-highlight">操作系统</a>. */  
    private String osName;   
        
    /** *//** 总的<a href="https://www.baidu.com/s?wd=%E7%89%A9%E7%90%86%E5%86%85%E5%AD%98&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYmhN-mHIWuWPhrHDYnyR40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHczPWTsPjn1rHDkPWTsrjfd" target="_blank" class="baidu-highlight">物理内存</a>. */  
    private long totalMemorySize;   
        
    /** *//** 剩余的<a href="https://www.baidu.com/s?wd=%E7%89%A9%E7%90%86%E5%86%85%E5%AD%98&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYmhN-mHIWuWPhrHDYnyR40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHczPWTsPjn1rHDkPWTsrjfd" target="_blank" class="baidu-highlight">物理内存</a>. */  
    private long freePhysicalMemorySize;   
        
    /** *//** 已使用的<a href="https://www.baidu.com/s?wd=%E7%89%A9%E7%90%86%E5%86%85%E5%AD%98&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYmhN-mHIWuWPhrHDYnyR40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHczPWTsPjn1rHDkPWTsrjfd" target="_blank" class="baidu-highlight">物理内存</a>. */  
    private long usedMemory;   
        
    /** *//** 线程总数. */  
    private int totalThread;   
        
    /** *//** <a href="https://www.baidu.com/s?wd=cpu%E4%BD%BF%E7%94%A8%E7%8E%87&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YYmhN-mHIWuWPhrHDYnyR40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHczPWTsPjn1rHDkPWTsrjfd" target="_blank" class="baidu-highlight">cpu使用率</a>. */  
    private double cpuRatio;   
   
    public long getFreeMemory() {   
        return freeMemory;   
    }   
   
    public void setFreeMemory(long freeMemory) {   
        this.freeMemory = freeMemory;   
    }   
   
    public long getFreePhysicalMemorySize() {   
        return freePhysicalMemorySize;   
    }   
   
    public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {   
        this.freePhysicalMemorySize = freePhysicalMemorySize;   
    }   
   
    public long getMaxMemory() {   
        return maxMemory;   
    }   
   
    public void setMaxMemory(long maxMemory) {   
        this.maxMemory = maxMemory;   
    }   
   
    public String getOsName() {   
        return osName;   
    }   
   
    public void setOsName(String osName) {   
        this.osName = osName;   
    }   
   
    public long getTotalMemory() {   
        return totalMemory;   
    }   
   
    public void setTotalMemory(long totalMemory) {   
        this.totalMemory = totalMemory;   
    }   
   
    public long getTotalMemorySize() {   
        return totalMemorySize;   
    }   
   
    public void setTotalMemorySize(long totalMemorySize) {   
        this.totalMemorySize = totalMemorySize;   
    }   
   
    public int getTotalThread() {   
        return totalThread;   
    }   
   
    public void setTotalThread(int totalThread) {   
        this.totalThread = totalThread;   
    }   
   
    public long getUsedMemory() {   
        return usedMemory;   
    }   
   
    public void setUsedMemory(long usedMemory) {   
        this.usedMemory = usedMemory;   
    }   
   
    public double getCpuRatio() {   
        return cpuRatio;   
    }   
   
    public void setCpuRatio(double cpuRatio) {   
        this.cpuRatio = cpuRatio;   
    }   
}
posted @ 2017-07-04 00:48  不会就问咯  阅读(3322)  评论(0编辑  收藏  举报