獲得JVM的總內存,最大內存,以及空閑內存

lplo 9年前發布 | 2K 次閱讀 Java

要獲得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(); } }</pre>
 本文由用戶 lplo 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!