JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

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) 2010 by Dr. Herong Yang, herongyang.com
 */
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());
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java RuntimeMemory
# of processors: 2
 Maximum memory: 66650112
   Total memory: 5177344
    Free memory: 4993136

The test result tells me that:

  • There are 2 CPUs on my computer, which is correct.
  • The maximum memory reserved for the JVM is 66,650,112 bytes, about 64 MB.
  • The total memory allocated to the JVM is 5,177,344 bytes, about 5 MB.
  • The free memory available for new objects is 4,993,136 bytes. So the JVM itself used only about 184,208 bytes, less than 200 KB.

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

C:\herong\jvm>java -Xmx8m RuntimeMemory
# of processors: 2
 Maximum memory: 8323072
   Total memory: 5177344
    Free memory: 4993136

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

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

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Printing Runtime Basic Information