Java gets Linux system CPU usage

Source: Internet
Author: User

Original: http://www.open-open.com/code/view/1426152165201

ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.util.StringTokenizer;Importorg.apache.commons.io.FileSystemUtils; Public classOsutils {/*** Function: Available disk **/     Public Static intdisk () {Try {            LongTotal = filesystemutils.freespacekb ("/Home"); DoubleDisk = (Double) total/1024/1024; return(int) disk; } Catch(IOException e) {e.printstacktrace (); }        return0; }    /*** Function: Get CPU utilization of Linux system **/     Public Static intCPUUsage () {Try{Map<?,? > MAP1 =Osutils.cpuinfo (); Thread.Sleep (5 * 1000); Map<?,? > MAP2 =Osutils.cpuinfo (); LongUser1 = Long.parselong (Map1.get ("User"). toString ()); LongNice1 = Long.parselong (Map1.get ("nice"). toString ()); LongSystem1 = Long.parselong (Map1.get ("System"). toString ()); LongIdle1 = Long.parselong (Map1.get ("Idle"). toString ()); LongUser2 = Long.parselong (Map2.get ("User"). toString ()); LongNice2 = Long.parselong (Map2.get ("nice"). toString ()); LongSYSTEM2 = Long.parselong (Map2.get ("System"). toString ()); LongIdle2 = Long.parselong (Map2.get ("Idle"). toString ()); LongTotal1 = user1 + system1 +Nice1; LongTotal2 = user2 + system2 +Nice2; floatTotal = Total2-Total1; LongTotalIdle1 = user1 + Nice1 + system1 +Idle1; LongTotalIdle2 = user2 + nice2 + system2 +Idle2; floatTotalidle = TotalIdle2-TotalIdle1; floatCpusage = (total/totalidle) * 100; return(int) Cpusage; } Catch(interruptedexception e) {e.printstacktrace (); }        return0; }    /*** Function: CPU usage information **/     Public Staticmap<?,? >Cpuinfo () {InputStreamReader inputs=NULL; BufferedReader Buffer=NULL; Map<string, object> map =NewHashmap<string, object>(); Try{Inputs=NewInputStreamReader (NewFileInputStream ("/proc/stat")); Buffer=NewBufferedReader (inputs); String Line= "";  while(true) { line=Buffer.readline (); if(line = =NULL) {                     Break; }                if(Line.startswith ("CPU") {StringTokenizer Tokenizer=NewStringTokenizer (line); List<String> temp =NewArraylist<string>();  while(Tokenizer.hasmoreelements ()) {String value=Tokenizer.nexttoken ();                    Temp.add (value); } map.put ("User", Temp.get (1)); Map.put ("Nice", Temp.get (2)); Map.put ("System", Temp.get (3)); Map.put ("Idle", Temp.get (4)); Map.put ("Iowait", Temp.get (5)); Map.put ("IRQ", Temp.get (6)); Map.put ("Softirq", Temp.get (7)); Map.put ("Stealstolen", Temp.get (8));  Break; }            }        } Catch(Exception e) {e.printstacktrace (); } finally {            Try{buffer.close ();            Inputs.close (); } Catch(Exception E2) {e2.printstacktrace (); }        }        returnmap; }    /*** Function: Memory utilization **/     Public Static intMemoryusage () {Map<string, object> map =NewHashmap<string, object>(); InputStreamReader Inputs=NULL; BufferedReader Buffer=NULL; Try{Inputs=NewInputStreamReader (NewFileInputStream ("/proc/meminfo")); Buffer=NewBufferedReader (inputs); String Line= "";  while(true) { line=Buffer.readline (); if(line = =NULL)                     Break; intBeginindex = 0; intEndIndex = Line.indexof (":"); if(EndIndex! =-1) {String key=line.substring (Beginindex, EndIndex); Beginindex= EndIndex + 1; EndIndex=line.length (); String Memory=line.substring (Beginindex, EndIndex); String value= Memory.replace ("KB", "" "). Trim ();                Map.put (key, value); }            }            LongMemtotal = Long.parselong (Map.get ("Memtotal"). toString ()); LongMemfree = Long.parselong (Map.get ("Memfree"). toString ()); Longmemused = Memtotal-Memfree; Longbuffers = Long.parselong (Map.get ("Buffers"). toString ()); Longcached = Long.parselong (Map.get ("Cached"). toString ()); DoubleUsage = (Double) (memused-buffers-cached)/memtotal * 100; return(int) usage; } Catch(Exception e) {e.printstacktrace (); } finally {            Try{buffer.close ();            Inputs.close (); } Catch(Exception E2) {e2.printstacktrace (); }        }        return0; }}

Java gets Linux system CPU usage

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.