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