Java.lang Package Classes

Java Runtime - freeMemory() Method



The java.util.Runtime.freeMemory() method returns the amount of free memory in the Java Virtual Machine. Calling the gc method may result in increasing the value returned by freeMemory.

Syntax

public long freeMemory()

Parameters

No parameter is required.

Return Value

Returns an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.

Exception

NA.

Example:

In the example below, the java.util.Runtime.freeMemory() method is used to get the amount of free memory in the Java Virtual Machine.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //print the free memory in JVM
    System.out.print("Approx. free memory in bytes: ");
    System.out.print(Runtime.getRuntime().freeMemory());
  }
}

The possible output of the above code could be:

Approx. free memory in bytes: 9417808

❮ Java.lang - Runtime