CPU Usage:
InputStream is = null;
InputStreamReader ISR = null;
BufferedReader brstat = null;
StringTokenizer tokenstat = null; The application class used to separate string
try {
System.out.println ("Get usage rate of CUP:");
Process process = Runtime.getruntime (). EXEC ("Top-b"); The top command is a common performance analysis tool under Linux that shows the resource usage of each process in the system in real time.
is = Process.getinputstream (); The contents of the System.out.print output of the newly opened program can be read
ISR = new InputStreamReader (IS); Converts a byte stream to a character stream.
Brstat = new BufferedReader (ISR); BufferedReader Stream can read the line of text
Brstat.readline ();
Brstat.readline ();
Tokenstat = new StringTokenizer (Brstat.readline ());
Tokenstat.nexttoken ();
System.out.println ("Percentage of CPU occupied by user space:" + Tokenstat.nexttoken ());
Tokenstat.nexttoken ();
System.out.println ("Percentage of CPU consumed by kernel space:" + Tokenstat.nexttoken ());
Tokenstat.nexttoken ();
Tokenstat.nexttoken ();
Tokenstat.nexttoken ();
String Cpufree = Tokenstat.nexttoken ();
System.out.println ("Percentage of idle CPU:" + cpufree);
float free = new float (free);
Float usage=1-free.floatvalue ()/100
Memory:
Operatingsystemmxbean OSMXB = (Operatingsystemmxbean) managementfactory
. Getoperatingsystemmxbean ();
Virtual memory
Long totalvirtualmemory = osmxb.gettotalswapspacesize ()/MB;
Total Physical Memory
Long totalmemorysize = osmxb.gettotalphysicalmemorysize ()/MB;
The remaining physical memory
Long freephysicalmemorysize = osmxb.getfreephysicalmemorysize ()/MB;
Physical memory already in use
Long usedmemory = (osmxb.gettotalphysicalmemorysize ()-OSMXB
. Getfreephysicalmemorysize ())/MB;
Disk space:
Process pro = null;
Runtime r = Runtime.getruntime ();
String command = "df-h";
Pro = r.exec (command);
BufferedReader in = new BufferedReader (New InputStreamReader (
Pro.getinputstream ()));
String line = null;
System.out.println ("Currently available space and use case for all file systems Filesystem Size used Avail use% mounted on");
while (line = In.readline ()) = null) {
string[] Info = Line.split ("\\s+");
System.out.println (In.readline ());
}
In.close ();
Pro.destroy ();
Linux under Java gets CPU, memory, disk IO, network bandwidth usage
Method: Linux uses Java to get CPU, memory, disk system resource information