Printing Runtime Basic Information

This section provides a tutorial example on how to access the Runtime instance of a running JVM and print some basic information.

To see how to access the Runtime instance of a running JVM and get some basic information, I wrote this simple Runtime test program, RuntimeMemory.java:

/* RuntimeMemory.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class RuntimeMemory {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;

      // getting the Runtime instance of the JVM
      Runtime rt = Runtime.getRuntime();

      // obtaining basic information
      out.println("# of processors: " + rt.availableProcessors());
      out.println(" Maximum memory: " + rt.maxMemory());
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());
      out.println("    Used memory: "
         + (rt.totalMemory()-rt.freeMemory()));
   }
}

If I run it with the JVM in JDK 17 on my Windows computer, I am getting the following output:

herong> java RuntimeMemory.java

# of processors: 8
 Maximum memory: 1050673152
   Total memory: 67108864
    Free memory: 50331648
    Used memory: 16777216

This tells me that:

As references, I collected outputs of RuntimeMemory.java running on different versions JVMs on different computers:

JVM/JDK            # of                      Memory
Version  OS       Proc.     Maximum      Total       Free      Used
-------  -------  -----  ----------  ---------  ---------  --------
 20      macOS        8  4294967296  272629760  252405976  20223784
 17      Windows      8  1050673152   67108864   50331648  16777216
 15      Windows      8  1050673152   67108864   47487912  19620944
 14      macOS        8  4294967296  270532608  252471760  18060848
 10.0.1  Windows      4  1006632960   62914560   60817408   2097152
  1.8.0  Windows      4   259522560   16252928   15800200    452728
  1.7.0  Windows      4   259522560   16252928   15964936    287992
  1.6.0  Windows      2    66650112    5177344    4993136    184208

As you can see, the newer version of JVM always uses more memory than older versions.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

java.lang.Runtime Class - The JVM Instance

 What Is Runtime?

Printing Runtime Basic Information

 Running the Garbage Collector Explicitly

 Shutting Down or Terminating the JVM Instance

 Executing System Commands

 Loading Native Libraries

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB