Java Runtime getRuntime() 方法



描述

Java Runtime getRuntime() 方法返回與當前 Java 應用程式關聯的執行時物件。 Runtime 類的許多方法都是例項方法,必須針對當前執行時物件呼叫。

宣告

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

public static Runtime getRuntime()

引數

返回值

此方法返回與當前 Java 應用程式關聯的 Runtime 物件。

異常

示例:獲取表示當前環境的 Runtime

以下示例演示了 Java Runtime getRuntime() 方法的用法。在這個程式中,我們使用 getRuntime() 獲取了 Runtime 物件,然後使用 Runtime 物件的 freeMemory() 方法列印可用記憶體。

package com.tutorialspoint;

public class RuntimeDemo {

   public static void main(String[] args) {

      // print when the program starts
      System.out.println("Program starting...");

      // get the current runtime assosiated with this process
      Runtime run = Runtime.getRuntime();

      // print the current free memory for this runtime
      System.out.println(run.freeMemory());
   }
}

輸出

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

Program starting...
62780856
java_lang_runtime.htm
廣告
© . All rights reserved.