The code for Java to obtain the current system memory is as follows:
Import java. Lang. Management. managementfactory;
Import com. Sun. Management. operatingsystemmxbean;
Public class ostest {
Public static void main (string [] ARGs ){
Operatingsystemmxbean osmb = (operatingsystemmxbean) managementfactory. getoperatingsystemmxbean ();
System. Out. println ("total system physical memory" + osmb. gettotalphysicalmemorysize ()/1024/1024 + "MB ");
System. Out. println ("total physical available memory of the system:" + osmb. getfreephysicalmemorysize ()/1024/1024 + "MB ");
}
}
Managementfactory. getoperatingsystemmxbean () returns operatingsystemmxbean in Java. Lang. Management;
We want to use Com. Sun. Management. operatingsystemmxbean;
In the Java class library, you can find:
Public abstract interface com. Sun. Management. operatingsystemmxbean extends java. Lang. Management. operatingsystemmxbean
So we can force the conversion.
If you want to get the disk usage, you can refer to the example on the third floor. To obtain the system information, use system. getproperty (string key );
Method.
Example of disk usage in jdk6.0
Import java. Io. file;
/***** Example of disk usage in jdk6.0 */
Public class diskfree {
Public static void main (string [] ARGs ){
File [] Roots = file. listroots ();
// Obtain the list of Disk Partitions
For (File file: roots ){
System. Out. println (file. getpath () + "information :");
System. Out. println ("Idle unused =" + file. getfreespace ()/1024/1024
/1024 + "G"); // free space
System. Out. println ("used =" + file. getusablespace ()/1024/1024
/1024 + "G"); // available space
System. Out. println ("total capacity =" + file. gettotalspace ()/1024/1024
/1024 + "G"); // total space
System. Out. println ();
}
}
}