java 获取系统信息及CPU的使用率

原文:http://kakaluyi.javaeye.com/blog/211492

最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o...

在Java中,可以获得总的物理内存、剩余的物理内存、已使用的物理内存等信息,下面例子可以取得这些信息,并且获得在Windows下的内存使用率。
     首先编写一个MonitorInfoBean类,用来装载监控的一些信息,包括物理内存、剩余的物理内存、已使用的物理内存、内存使用率等字段,该类的代码如下:
Java代码 复制代码

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;   
           
        /** *//** 操作系统. */  
        private String osName;   
           
        /** *//** 总的物理内存. */  
        private long totalMemorySize;   
           
        /** *//** 剩余的物理内存. */  
        private long freePhysicalMemorySize;   
           
        /** *//** 已使用的物理内存. */  
        private long usedMemory;   
           
        /** *//** 线程总数. */  
        private int totalThread;   
           
        /** *//** cpu使用率. */  
        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;   
        }   
    }

[java] view plain copy

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;  
          
        /** *//** 操作系统. */  
        private String osName;  
          
        /** *//** 总的物理内存. */  
        private long totalMemorySize;  
          
        /** *//** 剩余的物理内存. */  
        private long freePhysicalMemorySize;  
          
        /** *//** 已使用的物理内存. */  
        private long usedMemory;  
          
        /** *//** 线程总数. */  
        private int totalThread;  
          
        /** *//** cpu使用率. */  
        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;  
        }  
    }

接着编写一个获得当前的监控信息的接口,该类的代码如下所示:
Java代码 复制代码

package com.amgkaka.performance;   
      
    /** *//**  
     * 获取系统信息的业务逻辑类接口.  
     * @author amg * @version 1.0   
     * Creation date: 2008-3-11 - 上午10:06:06  
     */  
    public interface IMonitorService {   
        /** *//**  
         * 获得当前的监控对象.  
         * @return 返回构造好的监控对象  
         * @throws Exception  
         * @author amgkaka  
         * Creation date: 2008-4-25 - 上午10:45:08  
         */  
        public MonitorInfoBean getMonitorInfoBean() throws Exception;   
      
    }

[java] view plain copy

package com.amgkaka.performance;  
      
    /** *//**
     * 获取系统信息的业务逻辑类接口.
     * @author amg * @version 1.0  
     * Creation date: 2008-3-11 - 上午10:06:06
     */  
    public interface IMonitorService {  
        /** *//**
         * 获得当前的监控对象.
         * @return 返回构造好的监控对象
         * @throws Exception
         * @author amgkaka
         * Creation date: 2008-4-25 - 上午10:45:08
         */  
        public MonitorInfoBean getMonitorInfoBean() throws Exception;  
      
    }

该类的实现类MonitorServiceImpl如下所示:
Java代码 复制代码

package com.amgkaka.performance;   
      
    import java.io.InputStreamReader;   
    import java.io.LineNumberReader;   
      
    import sun.management.ManagementFactory;   
      
    import com.sun.management.OperatingSystemMXBean;   
      
    /** *//**  
     * 获取系统信息的业务逻辑实现类.  
     * @author amg * @version 1.0 Creation date: 2008-3-11 - 上午10:06:06  
     */  
    public class MonitorServiceImpl implements IMonitorService {   
        //可以设置长些,防止读到运行此次系统检查时的cpu占用率,就不准了   
        private static final int CPUTIME = 5000;   
      
        private static final int PERCENT = 100;   
      
        private static final int FAULTLENGTH = 10;   
      
        /** *//**  
         * 获得当前的监控对象.  
         * @return 返回构造好的监控对象  
         * @throws Exception  
         * @author amg     * Creation date: 2008-4-25 - 上午10:45:08  
         */  
        public MonitorInfoBean getMonitorInfoBean() throws Exception {   
            int kb = 1024;   
               
            // 可使用内存   
            long totalMemory = Runtime.getRuntime().totalMemory() / kb;   
            // 剩余内存   
            long freeMemory = Runtime.getRuntime().freeMemory() / kb;   
            // 最大可使用内存   
            long maxMemory = Runtime.getRuntime().maxMemory() / kb;   
      
            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory   
                    .getOperatingSystemMXBean();   
      
            // 操作系统   
            String osName = System.getProperty("os.name");   
            // 总的物理内存   
            long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;   
            // 剩余的物理内存   
            long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;   
            // 已使用的物理内存   
            long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb   
                    .getFreePhysicalMemorySize())   
                    / kb;   
      
            // 获得线程总数   
            ThreadGroup parentThread;   
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread   
                    .getParent() != null; parentThread = parentThread.getParent())   
                ;   
            int totalThread = parentThread.activeCount();   
      
            double cpuRatio = 0;   
            if (osName.toLowerCase().startsWith("windows")) {   
                cpuRatio = this.getCpuRatioForWindows();   
            }   
               
            // 构造返回对象   
            MonitorInfoBean infoBean = new MonitorInfoBean();   
            infoBean.setFreeMemory(freeMemory);   
            infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);   
            infoBean.setMaxMemory(maxMemory);   
            infoBean.setOsName(osName);   
            infoBean.setTotalMemory(totalMemory);   
            infoBean.setTotalMemorySize(totalMemorySize);   
            infoBean.setTotalThread(totalThread);   
            infoBean.setUsedMemory(usedMemory);   
            infoBean.setCpuRatio(cpuRatio);   
            return infoBean;   
        }   
      
        /** *//**  
         * 获得CPU使用率.  
         * @return 返回cpu使用率  
         * @author amg     * Creation date: 2008-4-25 - 下午06:05:11  
         */  
        private double getCpuRatioForWindows() {   
            try {   
                String procCmd = System.getenv("windir")   
                        + "//system32//wbem//wmic.exe process get Caption,CommandLine,"  
                        + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";   
                // 取进程信息   
                long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));   
                Thread.sleep(CPUTIME);   
                long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));   
                if (c0 != null && c1 != null) {   
                    long idletime = c1[0] - c0[0];   
                    long busytime = c1[1] - c0[1];   
                    return Double.valueOf(   
                            PERCENT * (busytime) / (busytime + idletime))   
                            .doubleValue();   
                } else {   
                    return 0.0;   
                }   
            } catch (Exception ex) {   
                ex.printStackTrace();   
                return 0.0;   
            }   
        }   
      
        /** *//**  
         * 读取CPU信息.  
         * @param proc  
         * @return  
         * @author amg     * Creation date: 2008-4-25 - 下午06:10:14  
         */  
        private long[] readCpu(final Process proc) {   
            long[] retn = new long[2];   
            try {   
                proc.getOutputStream().close();   
                InputStreamReader ir = new InputStreamReader(proc.getInputStream());   
                LineNumberReader input = new LineNumberReader(ir);   
                String line = input.readLine();   
                if (line == null || line.length() < FAULTLENGTH) {   
                    return null;   
                }   
                int capidx = line.indexOf("Caption");   
                int cmdidx = line.indexOf("CommandLine");   
                int rocidx = line.indexOf("ReadOperationCount");   
                int umtidx = line.indexOf("UserModeTime");   
                int kmtidx = line.indexOf("KernelModeTime");   
                int wocidx = line.indexOf("WriteOperationCount");   
                long idletime = 0;   
                long kneltime = 0;   
                long usertime = 0;   
                while ((line = input.readLine()) != null) {   
                    if (line.length() < wocidx) {   
                        continue;   
                    }   
                    // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,   
                    // ThreadCount,UserModeTime,WriteOperation   
                    String caption = Bytes.substring(line, capidx, cmdidx - 1)   
                            .trim();   
                    String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();   
                    if (cmd.indexOf("wmic.exe") >= 0) {   
                        continue;   
                    }   
                    // log.info("line="+line);   
                    if (caption.equals("System Idle Process")   
                            || caption.equals("System")) {   
                        idletime += Long.valueOf(   
                                Bytes.substring(line, kmtidx, rocidx - 1).trim())   
                                .longValue();   
                        idletime += Long.valueOf(   
                                Bytes.substring(line, umtidx, wocidx - 1).trim())   
                                .longValue();   
                        continue;   
                    }   
      
                    kneltime += Long.valueOf(   
                            Bytes.substring(line, kmtidx, rocidx - 1).trim())   
                            .longValue();   
                    usertime += Long.valueOf(   
                            Bytes.substring(line, umtidx, wocidx - 1).trim())   
                            .longValue();   
                }   
                retn[0] = idletime;   
                retn[1] = kneltime + usertime;   
                return retn;   
            } catch (Exception ex) {   
                ex.printStackTrace();   
            } finally {   
                try {   
                    proc.getInputStream().close();   
                } catch (Exception e) {   
                    e.printStackTrace();   
                }   
            }   
            return null;   
        }   
           
        /** *//**  
         * 测试方法.  
         * @param args  
         * @throws Exception  
         * @author amg     * Creation date: 2008-4-30 - 下午04:47:29  
         */  
        public static void main(String[] args) throws Exception {   
            IMonitorService service = new MonitorServiceImpl();   
            MonitorInfoBean monitorInfo = service.getMonitorInfoBean();   
            System.out.println("cpu占有率=" + monitorInfo.getCpuRatio());   
               
            System.out.println("可使用内存=" + monitorInfo.getTotalMemory());   
            System.out.println("剩余内存=" + monitorInfo.getFreeMemory());   
            System.out.println("最大可使用内存=" + monitorInfo.getMaxMemory());   
               
            System.out.println("操作系统=" + monitorInfo.getOsName());   
            System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "kb");   
            System.out.println("剩余的物理内存=" + monitorInfo.getFreeMemory() + "kb");   
            System.out.println("已使用的物理内存=" + monitorInfo.getUsedMemory() + "kb");   
            System.out.println("线程总数=" + monitorInfo.getTotalThread() + "kb");   
        }   
    }

[java] view plain copy

package com.amgkaka.performance;  
      
    import java.io.InputStreamReader;  
    import java.io.LineNumberReader;  
      
    import sun.management.ManagementFactory;  
      
    import com.sun.management.OperatingSystemMXBean;  
      
    /** *//**
     * 获取系统信息的业务逻辑实现类.
     * @author amg * @version 1.0 Creation date: 2008-3-11 - 上午10:06:06
     */  
    public class MonitorServiceImpl implements IMonitorService {  
        //可以设置长些,防止读到运行此次系统检查时的cpu占用率,就不准了  
        private static final int CPUTIME = 5000;  
      
        private static final int PERCENT = 100;  
      
        private static final int FAULTLENGTH = 10;  
      
        /** *//**
         * 获得当前的监控对象.
         * @return 返回构造好的监控对象
         * @throws Exception
         * @author amg     * Creation date: 2008-4-25 - 上午10:45:08
         */  
        public MonitorInfoBean getMonitorInfoBean() throws Exception {  
            int kb = 1024;  
              
            // 可使用内存  
            long totalMemory = Runtime.getRuntime().totalMemory() / kb;  
            // 剩余内存  
            long freeMemory = Runtime.getRuntime().freeMemory() / kb;  
            // 最大可使用内存  
            long maxMemory = Runtime.getRuntime().maxMemory() / kb;  
      
            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory  
                    .getOperatingSystemMXBean();  
      
            // 操作系统  
            String osName = System.getProperty("os.name");  
            // 总的物理内存  
            long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;  
            // 剩余的物理内存  
            long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;  
            // 已使用的物理内存  
            long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb  
                    .getFreePhysicalMemorySize())  
                    / kb;  
      
            // 获得线程总数  
            ThreadGroup parentThread;  
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread  
                    .getParent() != null; parentThread = parentThread.getParent())  
                ;  
            int totalThread = parentThread.activeCount();  
      
            double cpuRatio = 0;  
            if (osName.toLowerCase().startsWith("windows")) {  
                cpuRatio = this.getCpuRatioForWindows();  
            }  
              
            // 构造返回对象  
            MonitorInfoBean infoBean = new MonitorInfoBean();  
            infoBean.setFreeMemory(freeMemory);  
            infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);  
            infoBean.setMaxMemory(maxMemory);  
            infoBean.setOsName(osName);  
            infoBean.setTotalMemory(totalMemory);  
            infoBean.setTotalMemorySize(totalMemorySize);  
            infoBean.setTotalThread(totalThread);  
            infoBean.setUsedMemory(usedMemory);  
            infoBean.setCpuRatio(cpuRatio);  
            return infoBean;  
        }  
      
        /** *//**
         * 获得CPU使用率.
         * @return 返回cpu使用率
         * @author amg     * Creation date: 2008-4-25 - 下午06:05:11
         */  
        private double getCpuRatioForWindows() {  
            try {  
                String procCmd = System.getenv("windir")  
                        + "//system32//wbem//wmic.exe process get Caption,CommandLine,"  
                        + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";  
                // 取进程信息  
                long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));  
                Thread.sleep(CPUTIME);  
                long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));  
                if (c0 != null && c1 != null) {  
                    long idletime = c1[0] - c0[0];  
                    long busytime = c1[1] - c0[1];  
                    return Double.valueOf(  
                            PERCENT * (busytime) / (busytime + idletime))  
                            .doubleValue();  
                } else {  
                    return 0.0;  
                }  
            } catch (Exception ex) {  
                ex.printStackTrace();  
                return 0.0;  
            }  
        }  
      
        /** *//**
         * 读取CPU信息.
         * @param proc
         * @return
         * @author amg     * Creation date: 2008-4-25 - 下午06:10:14
         */  
        private long[] readCpu(final Process proc) {  
            long[] retn = new long[2];  
            try {  
                proc.getOutputStream().close();  
                InputStreamReader ir = new InputStreamReader(proc.getInputStream());  
                LineNumberReader input = new LineNumberReader(ir);  
                String line = input.readLine();  
                if (line == null || line.length() < FAULTLENGTH) {  
                    return null;  
                }  
                int capidx = line.indexOf("Caption");  
                int cmdidx = line.indexOf("CommandLine");  
                int rocidx = line.indexOf("ReadOperationCount");  
                int umtidx = line.indexOf("UserModeTime");  
                int kmtidx = line.indexOf("KernelModeTime");  
                int wocidx = line.indexOf("WriteOperationCount");  
                long idletime = 0;  
                long kneltime = 0;  
                long usertime = 0;  
                while ((line = input.readLine()) != null) {  
                    if (line.length() < wocidx) {  
                        continue;  
                    }  
                    // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,  
                    // ThreadCount,UserModeTime,WriteOperation  
                    String caption = Bytes.substring(line, capidx, cmdidx - 1)  
                            .trim();  
                    String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();  
                    if (cmd.indexOf("wmic.exe") >= 0) {  
                        continue;  
                    }  
                    // log.info("line="+line);  
                    if (caption.equals("System Idle Process")  
                            || caption.equals("System")) {  
                        idletime += Long.valueOf(  
                                Bytes.substring(line, kmtidx, rocidx - 1).trim())  
                                .longValue();  
                        idletime += Long.valueOf(  
                                Bytes.substring(line, umtidx, wocidx - 1).trim())  
                                .longValue();  
                        continue;  
                    }  
      
                    kneltime += Long.valueOf(  
                            Bytes.substring(line, kmtidx, rocidx - 1).trim())  
                            .longValue();  
                    usertime += Long.valueOf(  
                            Bytes.substring(line, umtidx, wocidx - 1).trim())  
                            .longValue();  
                }  
                retn[0] = idletime;  
                retn[1] = kneltime + usertime;  
                return retn;  
            } catch (Exception ex) {  
                ex.printStackTrace();  
            } finally {  
                try {  
                    proc.getInputStream().close();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
            return null;  
        }  
          
        /** *//**
         * 测试方法.
         * @param args
         * @throws Exception
         * @author amg     * Creation date: 2008-4-30 - 下午04:47:29
         */  
        public static void main(String[] args) throws Exception {  
            IMonitorService service = new MonitorServiceImpl();  
            MonitorInfoBean monitorInfo = service.getMonitorInfoBean();  
            System.out.println("cpu占有率=" + monitorInfo.getCpuRatio());  
              
            System.out.println("可使用内存=" + monitorInfo.getTotalMemory());  
            System.out.println("剩余内存=" + monitorInfo.getFreeMemory());  
            System.out.println("最大可使用内存=" + monitorInfo.getMaxMemory());  
              
            System.out.println("操作系统=" + monitorInfo.getOsName());  
            System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "kb");  
            System.out.println("剩余的物理内存=" + monitorInfo.getFreeMemory() + "kb");  
            System.out.println("已使用的物理内存=" + monitorInfo.getUsedMemory() + "kb");  
            System.out.println("线程总数=" + monitorInfo.getTotalThread() + "kb");  
        }  
    }

该实现类中需要用到一个自己编写byte的工具类,该类的代码如下所示:
Java代码 复制代码

package com.amgkaka.performance;   
      
    /** *//**  
     * byte操作类.  
     * @author amg * @version 1.0   
     * Creation date: 2008-4-30 - 下午04:57:23  
     */  
    public class Bytes {   
        /** *//**  
         * 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在  
         * 包含汉字的字符串时存在隐患,现调整如下:  
         * @param src 要截取的字符串  
         * @param start_idx 开始坐标(包括该坐标)  
         * @param end_idx   截止坐标(包括该坐标)  
         * @return  
         */  
        public static String substring(String src, int start_idx, int end_idx){   
            byte[] b = src.getBytes();   
            String tgt = "";   
            for(int i=start_idx; i<=end_idx; i++){   
                tgt +=(char)b[i];   
            }   
            return tgt;   
        }   
    }

[java] view plain copy

package com.amgkaka.performance;  
      
    /** *//**
     * byte操作类.
     * @author amg * @version 1.0  
     * Creation date: 2008-4-30 - 下午04:57:23
     */  
    public class Bytes {  
        /** *//**
         * 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在
         * 包含汉字的字符串时存在隐患,现调整如下:
         * @param src 要截取的字符串
         * @param start_idx 开始坐标(包括该坐标)
         * @param end_idx   截止坐标(包括该坐标)
         * @return
         */  
        public static String substring(String src, int start_idx, int end_idx){  
            byte[] b = src.getBytes();  
            String tgt = "";  
            for(int i=start_idx; i<=end_idx; i++){  
                tgt +=(char)b[i];  
            }  
            return tgt;  
        }  
    }

运行下MonitorBeanImpl类,读者将会看到当前的内存、cpu利用率等信息

wmic很强大,网上有很多wmic的命令,

eg:wmic 获取物理内存
wmic memlogical get TotalPhysicalMemory

wmic 获取进程信息,很详细

wmic process

System.getProperty("os.name"));//得到操作系统名字
System.getProperty("sun.os.patch.level");//得到操作系统版本

java 获取系统信息及CPU的使用率(转)的更多相关文章

  1. Java获取Linux系统cpu使用率

    原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...

  2. JAVA获取系统信息以及系统时间

    在做测试的时候,经常需要获取系统信息,并且用获取到的系统时间给生成的报告取名字. 以下代码实在TestNG展示的,没有Test NG的话需要些一个main方法. import java.net.Ine ...

  3. 关于Java获取系统信息

    本文部分转载自: http://www.cnblogs.com/wuhenke/archive/2011/11/19/2255400.html 我总结的相关类似博客:http://www.cnblog ...

  4. 获取系统信息(CPU、内存等)

    简述 获取计算机CPU.主板.内存.硬盘.网卡这些信息,Qt中没有相应的处理,所以需要根据平台来做差异化处理.也许Qt为了跨平台,没有提供与操作系统和硬件密切相关的一些功能(如内存.CPU.硬盘等相关 ...

  5. java获取系统信息

    public class SystemInfo { public static void main(String[] args) { //系统属性 Properties prop = System.g ...

  6. Java获取系统信息(用户目录,临时目录等)

    java.version Java运行时环境版本 java.vendor Java运行时环境供应商 java.vendor.url Java供应商的 URL java.home Java安装目录 ja ...

  7. Java如何获取系统信息(包括操作系统、jvm、cpu、内存、硬盘、网络、io等)

    1 下载安装sigar-1.6.4.zip 使用java自带的包获取系统数据,容易找不到包,尤其是内存信息不够准确,所以选择使用sigar获取系统信息. 下载地址:http://sourceforge ...

  8. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  9. JAVA获取计算机CPU、硬盘、主板、网络等信息

    通过使用第三方开源jar包sigar.jar我们可以获得本地的信息 1.下载sigar.jar sigar官方主页 sigar-1.6.4.zip 2.按照主页上的说明解压包后将相应的文件copy到j ...

随机推荐

  1. @Resource注解完成自动装配

    @Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...

  2. __cdecl,__stdcall,__fastcall,__pascal,__thiscall 的区别

    关于函数的调用规则(调用约定),大多数时候是不需要了解的,但是如果需要跨语言的编程,比如VC写的dll要delphi调用,则需要了解. microsoft的vc默认的是__cdecl方式,而windo ...

  3. Springmvc 流程图

  4. 使用Idea远程部署调试tomcat

    转自:http://blog.csdn.net/jane1229/article/details/52402119 远程服务器的配置: 1.在远程服务器安装jdk和tomcat 2.配置环境变量 PA ...

  5. 51nod 1791 合法括号子段

    有一个括号序列,现在要计算一下它有多少非空子段是合法括号序列. 合法括号序列的定义是: 1.空序列是合法括号序列. 2.如果S是合法括号序列,那么(S)是合法括号序列.3.如果A和B都是合法括号序列, ...

  6. CCCC练习即感

    字符串进行初始化时不能通过char a[10]={'\0'}来简单进行,写循环或者memset,亲测有效,以及初始化分好情况,用空格还是'\0',别乱搞. 有一个有意思的题,连续因子,从2开始,依次向 ...

  7. centos 下构建lamp环境

    构建准备: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp - ...

  8. set .net principle

    var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(FormsA ...

  9. bzoj 1951 lucas crt 费马小定理

    首先假设输入的是n,m 我们就是要求m^(Σ(c(n,i) i|n)) mod p 那么根据费马小定理,上式等于 m^(Σ(c(n,i) i|n) mod  (p-1)) mod p 那么问题的关键就 ...

  10. .NET中zip的压缩和解压

    在.NET可以通过多种方式实现zip的压缩和解压:1.使用System.IO.Packaging:2.使用第三方类库:3.通过 System.IO.Compression 命名空间中新增的ZipArc ...