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.