获得JVM的总内存,最大内存,以及空闲内存
2018-07-20 来源:open-open
要获得jvm相关的内存信息,需要使用Runtime类的totalMemory(), maxMemory() 和 freeMemory()方法。
import java.text.DecimalFormat;
/**
* Main.java
*/
public class Main {
/**
* Displays the total amount of memory, the maximal amount of memory
* and the total amount of free memory in the Java Virtual Machine.
*/
public void displayAvailableMemory() {
DecimalFormat df = new DecimalFormat("0.00") ;
//Display the total amount of memory in the Java virtual machine.
long totalMem = Runtime.getRuntime().totalMemory();
System.out.println(df.format(totalMem 1000000F) + " MB");
//Display the maximum amount of memory that the Java virtual machine will attempt to use.
long maxMem = Runtime.getRuntime().maxMemory();
System.out.println(df.format(maxMem 1000000F) + " MB");
//Display the amount of free memory in the Java Virtual Machine.
long freeMem = Runtime.getRuntime().freeMemory();
System.out.println(df.format(freeMem 1000000F) + " MB");
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().displayAvailableMemory();
}
}
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:java堆排序算法代码
最新资讯
热门推荐