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. eclipse关闭错误警告提示

  2. SQL优化单表案例

    数据准备: -- 创建数据库 mysql> create database db_index_case; Query OK, row affected (0.00 sec) -- 查看数据库 m ...

  3. [BZOJ3033]太鼓达人|欧拉图

    Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk.Poet_shy和ly ...

  4. GUI自动化模块化实现方式

    效率为王:脚本与数据的解耦 + Page Object模型 1.数据驱动:实现了“测试脚本和数据的解耦”,数据驱动测试的数据文件中不仅可以包含测试输入数据,还可以包含测试验证结果数据,甚至可以包含测试 ...

  5. 【SQL】数据库更新

    1.插入 INSERT INTO R(A1,A2,...An) VALUES(v1, v2, ...,vn) 如果插入了所有属性,并且按照定义的顺序给出,可以省略(A1,A2,...An) 可以只插入 ...

  6. could not find or load the Qt platform plugin "xcb"

    没有解决 一些资料: https://wiki.qt.io/Install_Qt_5_on_Ubuntu http://doc.qt.io/qt-5/linux-requirements.html h ...

  7. 自动监控tomcat脚本并且执行重启操作

    #!/bin/sh # func:自动监控tomcat脚本并且执行重启操作 # author:reed # date:// # 定义环境变量 MYPATH=/usr/local/jdk/bin exp ...

  8. Bootstrap模态框垂直居中展示的方法

    在bootstrap.js中加入以下代码:

  9. python带cookie提交表单自动登录

    import urllib import urllib2 import cookielib login_url = "xxxxxxxxxxxxx" cj = cookielib.C ...

  10. NAT+穿洞基础知识梳理

    参考:https://www.cnblogs.com/shilxfly/p/6589255.html https://blog.csdn.net/phoenix06/article/details/7 ...