Java obtains the current system memory and drive letter size

Source: Internet
Author: User
Tags disk usage

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 ();
}
}

}

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.