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());
   }
}

If I compile and run it with the JVM in JDK 10, I am getting the following output:

C:\herong>\Progra~1\Java\jdk-10.0.1\bin\javac RuntimeMemory.java

C:\herong>\Progra~1\Java\jdk-10.0.1\bin\java RuntimeMemory
# of processors: 4
 Maximum memory: 1006632960
   Total memory: 62914560
    Free memory: 60817408

This tells me that:

Of course, I can lower the maximum memory by using the -Xmx command option:

C:\herong>\Progra~1\Java\jdk-10.0.1\bin\java -Xmx8m RuntimeMemory
# of processors: 4
 Maximum memory: 8388608
   Total memory: 8388608
    Free memory: 6581200

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

JVM/JDK   Year         # of                Memory
Version          Processors      Maximum    Total     Free
              
 10.0.1   2018            4   1006632960 62914560 60817408
  1.8.0   2016            4    259522560 16252928 15800200
  1.7.0   2013            4    259522560 16252928 15964936
  1.6.0   2007            2     66650112  5177344  4993136

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

Last update: 2018.

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

 JRockit JVM 28.2.7 by Oracle Corporation

 Outdated Tutorials

 References

 Full Version in PDF/EPUB