Get the amount of free memory in the Java Virtual Machine in Java

Description

The following code shows how to get the amount of free memory in the Java Virtual Machine.

Example


//w w  w  . j a  va 2s.co m
import java.text.DecimalFormat;

public class Main {
  public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");

    long freeMem = Runtime.getRuntime().freeMemory();
    System.out.println(df.format(freeMem / 1000000F) + " MB");
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization