Java執行時totalMemory()方法



描述

Java Runtime totalMemory() 方法返回 Java 虛擬機器中的總記憶體量。此方法返回的值可能會隨著時間的推移而變化,具體取決於主機環境。請注意,儲存任何給定型別的物件所需的記憶體量可能取決於實現。

宣告

以下是java.lang.Runtime.totalMemory()方法的宣告

public long totalMemory()

引數

返回值

此方法不返回值。

異常

示例:獲取當前環境的總記憶體(以位元組為單位)

以下示例演示了 Java Runtime totalMemory() 方法的使用。我們使用 totalMemory() 方法列印了當前環境的總記憶體。

package com.tutorialspoint;

public class RuntimeDemo {

   public static void main(String[] args) {

      // print the state of the program
      System.out.println("Program is starting...");

      // print the total memory
      System.out.println("" + Runtime.getRuntime().totalMemory());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Program is starting...
63111168
java_lang_runtime.htm
廣告
© . All rights reserved.