Java obtains CPU usage, memory usage, and hard disk usage.

Source: Internet
Author: User

Import java. Io. file;
Import java. Io. inputstreamreader;
Import java. Io. linenumberreader;
Import java. Lang. Management. managementfactory;
Import java. util. arraylist;
Import java. util. List;

Import com. Sun. Management. operatingsystemmxbean;

/**
* Obtain Windows System Information (CPU, memory, and file system)
*
* @ Author Libing
*
*/

Public class windowsinfoutil {
Private Static final int cputime = 500;
Private Static finals 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
. Getoperatingsystemmxbean ();
// Total physical memory + virtual memory
Long totalvirtualmemory = osmxb. gettotalswapspacesize ();
// Remaining physical memory
Long freephysicalmemorysize = osmxb. getfreephysicalmemorysize ();
Double compare = (double) (1-freephysicalmemorysize * 1.0
/Totalvirtualmemory) * 100;
String STR = "memory used:" + compare. intvalue () + "% ";
Return STR;
}

// Get the 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 + ": the disk has been used" + compare. intvalue () + "% ";
List. Add (STR );
}
}
Return list;
}

// Obtain the CPU usage
Public static string getcpuratioforwindows (){
Try {
String proccmd = system. getenv ("WINDIR ")
+ "// System32 // WBEM // wmic.exe
Process get caption, CommandLine, kernelmodetime, readoperationcount, threadcount, usermodetime, writeoperationcount ";
// Obtain Process Information
Long [] C0 = readcpu(runtime.getruntime(cmd.exe C (proccmd ));
Thread. Sleep (cputime );
Long [] C1 = readcpu(runtime.getruntime(cmd.exe C (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 usage:" + 0 + "% ";
}
} Catch (exception ex ){
Ex. printstacktrace ();
Return "CPU usage:" + 0 + "% ";
}
}

// Read CPU 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;
}
// Field order: caption, CommandLine, kernelmodetime, readoperationcount,
// Threadcount, usermodetime, writeoperation
String caption = substring (line, capidx, cmdidx-1). Trim ();
String cmd = 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;
}

/**
* Because string. substring has a problem in Chinese Character Processing (treat a Chinese character as a byte), there is a hidden danger in the string containing Chinese characters. The following adjustments are made:
*
* @ Param SRC
* String to be truncated
* @ Param start_idx
* Start coordinate (including the coordinate)
* @ Param end_idx
* Cutoff coordinates (including the coordinates)
* @ Return
*/
Private 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;
}
}

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.