Java gets CPU, memory, disk and other information

Source: Internet
Author: User

Java gets CPU, memory, disk and other information

Source: Java gets CPU, memory, disk and other information

Source code:http://www.zuidaima.com/share/1550463331306496.htm

Package Com.zuidaima.util;import Java.io.file;import Java.io.InputStreamReader; Import Java.io.LineNumberReader; Import java.util.ArrayList; Import java.util.List; Import sun.management.managementfactory;import com.sun.management.operatingsystemmxbean;/** * Get Windows System Information (CPU, memory     , file system) * @author www.zuidaima.com * */public class CPU {private static final int cputime = 500;     private static final int PERCENT = 100;     private static final int faultlength = 10;     public static void Main (string[] args) {System.out.println (Getcpuratioforwindows ());     System.out.println (Getmemery ()); System.out.println (Getdisk ()); }//Get Memory usage public static String getmemery () {Operatingsystemmxbean OSMXB = (Operatingsystemmxbean) managementfactory.ge   Toperatingsystemmxbean ();   Total Physical memory + virtual memory long totalvirtualmemory = Osmxb.gettotalswapspacesize ();  The remaining physical memory long freephysicalmemorysize = Osmxb.getfreephysicalmemorysize (); SYSTEM.OUT.PRINTLN ("Total Memory" + (totalvirtualmemory/1024.0/1024.0/1024.0));  SYSTEM.OUT.PRINTLN ("Memory Used" + ((totalvirtualmemory-freephysicalmemorysize)/1024.0/1024.0/1024.0));   Double compare= (double) (1-freephysicalmemorysize*1.0/totalvirtualmemory) *100;   String str= "Memory used:" +compare.intvalue () + "%"; return str; }//Get file system usage public static list<string> Getdisk () {//operating system list<string> list=new arraylist<string>   ();    for (char c = ' A '; c <= ' Z '; C + +) {String DirName = c + ":/";          File win = new file (dirName);           if (win.exists ()) {long total= (long) win.gettotalspace ();           Long free= (Long) win.getfreespace ();           Double compare= (double) (1-free*1.0/total) *100;           String str=c+ ": Disk has been used" +compare.intvalue () + "%";          List.add (str); }} return list; }//Get CPU Utilization public static String getcpuratioforwindows () {try {String proccmd = system.getenv ("WI Ndir ") +" \\system32\\wbem\\wmic.exe process get Caption,commandline,kernelmodetime,readoperatiOncount,threadcount,usermodetime,writeoperationcount ";              Fetch process information 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 "CPU Usage:" +double.valueof (PERCENT * (busytime) *1.0/(Busytime + idletime)). Intvalue () + "%";              } else {return "CPU utilization:" +0+ "%";              }} catch (Exception ex) {ex.printstacktrace ();          Return "CPU Usage:" +0+ "%";         }}//Read CPU-related information private static 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; }//fields appear in order: Caption,commandline,kernelmodetime,readoperationcount,//THR                 Eadcount,usermodetime,writeoperation String caption =substring (line, Capidx, cmdidx-1). Trim (); Stringcmd = substring (line, CMDIDX, kmtidx-1). Trim ();                 if (Cmd.indexof ("Wmic.exe") >= 0) {continue;                 } String S1 = substring (line, KMTIDX, rocidx-1). Trim ();                 String s2 = substring (line, UMTIDX, wocidx-1). Trim ();                         if (caption.equals ("System Idle Process") | | caption.equals ("SYSTEM")) {if (s1.length () > 0)                     Idletime + = long.valueof (S1). Longvalue ();                     if (s2.length () > 0) Idletime + = long.valueof (s2). Longvalue ();                 Continue                 } if (S1.length () > 0) Kneltime + = long.valueof (S1). Longvalue ();             if (s2.length () > 0) Usertime + = long.valueof (s2). 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;  }/** * Since string.substring has problems with Chinese character processing (treating a Chinese character as a byte), there is a hidden danger in the string containing Chinese characters, which is now adjusted as follows: * @author www.zuidaima.com * @param SRC string to intercept * @param start_idx start coordinates (including the coordinates) * @param end_idx cutoff coordinates (including the coordinates) * @return */private static Strin    G 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 gets CPU, memory, disk and other information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.