Java.lang Package Classes

Java Runtime - maxMemory() Method



The java.util.Runtime.maxMemory() method returns the maximum amount of memory that the Java virtual machine will attempt to use. If there is no inherent limit then the value Long.MAX_VALUE will be returned.

Syntax

public long maxMemory()

Parameters

No parameter is required.

Return Value

Returns the maximum amount of memory that the virtual machine will attempt to use, measured in bytes.

Exception

NA.

Example:

In the example below, the java.util.Runtime.maxMemory() method is used to get the maximum amount of memory that the Java virtual machine will attempt to use.

import java.lang.*;

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

The possible output of the above code could be:

Max amount of memory is: 148045824

❮ Java.lang - Runtime