Java.lang Package Classes

Java Runtime - totalMemory() Method



The java.util.Runtime.totalMemory() method returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment.

Syntax

public long totalMemory()

Parameters

No parameter is required.

Return Value

Returns the total amount of memory currently available for current and future objects, measured in bytes.

Exception

NA.

Example:

In the example below, the java.util.Runtime.totalMemory() method is used to get the total amount of memory in the Java virtual machine.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //print the total amount of memory in JVM
    System.out.print("Total amount of memory is: ");
    System.out.print(Runtime.getRuntime().totalMemory());
  }
}

The possible output of the above code could be:

Total amount of memory is: 10158080

❮ Java.lang - Runtime