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.