Display the amount of free memory in the Java Virtual Machine

Java 8Object Oriented ProgrammingProgramming

In order to display the amount of free memory in the Java Virtual Machine, we use the freeMemory() method. It is a method of the java.lang.Runtime Class. It returns the amount of free memory in the Java Virtual Machine. If we call the gc method, there is a possibility of increase of free memory.

Declaration − The java.lang.Runtime.freeMemory() method is declared as follows −

public long freeMemory()

Let us see a program to display the amount of free memory in the Java Virtual Machine −

Example

 Live Demo

public class Example {
   public static void main(String[] args) {
      // print statement at the start of the program
      System.out.println("Start...");
      // displays the free memory
      System.out.println("" + Runtime.getRuntime().freeMemory());
   }
}

Output

Start...
15732848
raja
Published on 08-Jan-2019 11:59:13
Advertisements