freeMemory() - Getting JVM Free Memory Information

This section provides a tutorial example on how to use freeMemory(), totalMemory(), and maxMemory() of the Runtime class to get JVM total and free memory information.

There a couple of values used in Java related to memory management:

There is one method in the Runtime class that you can use to get the each of those values. Here is a program showing you how to use those methods properly:

/**
 * FreeMemory.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
public class FreeMemory {
   private static long[] fm = new long[5];	
   private static long[] tm = new long[5];	
   private static long[] mm = new long[5];	
   private static long[] um = new long[5];	
   public static void main(String[] a) {
      Runtime rt = Runtime.getRuntime();
      getMemoryInfo(rt,0);
      getMemoryInfo(rt,1);
      getMemoryInfo(rt,2);
      getMemoryInfo(rt,3);
      getMemoryInfo(rt,4);
      printMemoryInfo(rt,0);
      printMemoryInfo(rt,1);
      printMemoryInfo(rt,2);
      printMemoryInfo(rt,3);
      printMemoryInfo(rt,4);
      for (int i=0; i<5; i++) {
         System.out.println("Get memory info - step = "+i);
         System.out.println("   Free memory = "+fm[i]);
         System.out.println("   Total memory = "+tm[i]);
         System.out.println("   Maximum memory = "+mm[i]);
         System.out.println("   Memory used = "+um[i]);
      }
   }
   public static void getMemoryInfo(Runtime rt, int i) {
      fm[i] = rt.freeMemory();
      tm[i] = rt.totalMemory();
      mm[i] = rt.maxMemory();
      um[i] = tm[i]-fm[i];
   }
   public static void printMemoryInfo(Runtime rt, int i) {
      long fm = rt.freeMemory();
      long tm = rt.totalMemory();
      long mm = rt.maxMemory();
      System.out.println("Print memory info - step = "+i);
      System.out.println("   Free memory = "+fm);
      System.out.println("   Total memory = "+tm);
      System.out.println("   Maximum memory = "+mm);
      System.out.println("   Memory used = "+(tm-fm));
   }
}

This program seems to be confusing:

You need to read the output of this program to answer those questions:

Print memory info - step = 0
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048
Print memory info - step = 1
   Free memory = 1774712
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 256904
Print memory info - step = 2
   Free memory = 1772496
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 259120
Print memory info - step = 3
   Free memory = 1770280
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 261336
Print memory info - step = 4
   Free memory = 1768064
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 263552
Get memory info - step = 0
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048
Get memory info - step = 1
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048
Get memory info - step = 2
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048
Get memory info - step = 3
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048
Get memory info - step = 4
   Free memory = 1777568
   Total memory = 2031616
   Maximum memory = 134217728
   Memory used = 254048

Note that:

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

System Properties and Runtime Object Methods

 JVM and OS System Properties

 System.setProperty() - Setting Your Own Properties

 Runtime.getRuntime() - Getting the Runtime Object

freeMemory() - Getting JVM Free Memory Information

 Calculating Memory Usage of an Array

 exec() - Executing Operating System Commands

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version