Method: Linux uses Java to get CPU, memory, disk system resource information

Source: Internet
Author: User

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

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.