1 import java.text.DecimalFormat; 2 3 public class Main { 4 /** 5 * 显示JVM总内存,JVM最大内存和总空闲内存 6 */ 7 public void displayAvailableMemory() { 8 DecimalFormat df = new DecimalFormat(“0.00″) ; 9 10 //显示JVM总内存 11 long totalMem = Runtime.getRuntime().totalMemory(); 12 System.out.println(df.format(totalMem 1000000F) + ” MB”); 13 //显示JVM尝试使用的最大内存 14 long maxMem = Runtime.getRuntime().maxMemory(); 15 System.out.println(df.format(maxMem 1000000F) + ” MB”); 16 //空闲内存 17 long freeMem = Runtime.getRuntime().freeMemory(); 18 System.out.println(df.format(freeMem 1000000F) + ” MB”); 19 } 20 21 /** 22 * Starts the program 23 * 24 * @param args the command line arguments 25 */ 26 public static void main(String[] args) { 27 new Main().displayAvailableMemory(); 28 } 29 }
时间: 2024-10-03 06:14:14