Runtime Class in Java
The Runtime class, in java.lang, represents the running JVM itself. Every Java program has exactly one Runtime object, obtained with Runtime.getRuntime(). It is used to ask how many processors are available, how much memory the heap has and uses, and to control the JVM: requesting garbage collection, adding shutdown hooks, running external programs and exiting. In this lesson, you will learn its methods and how to measure memory with it.
Methods of Runtime
| Method | Description |
|---|---|
Runtime.getRuntime() | It is used to get the single Runtime object. |
availableProcessors() | It returns the number of processors the JVM may use. |
totalMemory() | It returns the heap size currently reserved, in bytes. |
freeMemory() | It returns the unused part of totalMemory(). |
maxMemory() | It returns the most heap the JVM will ever use (-Xmx). |
gc() | It requests a garbage collection (same as System.gc()). |
addShutdownHook(thread) | It registers a thread to run at shutdown (see "Shutdown Hook in Java"). |
exec(...) | It starts an external program (prefer ProcessBuilder). |
exit(status) / halt(status) | It ends the JVM; halt skips shutdown hooks. |
Runtime.version() | It returns the Java version (static). |
How the Memory Numbers Relate
used = totalMemory() - freeMemory()
maxMemory() >= totalMemory() >= usedThe heap can grow from totalMemory() up to maxMemory() as needed.
Example: Measuring Memory Use
Let's see an example that reports processors and memory, and measures how much heap one million Integer objects take.
public class RuntimeDemo {
static long usedMb(Runtime rt) {
return (rt.totalMemory() - rt.freeMemory()) / (1024 * 1024);
}
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
System.out.println("Java version: " + Runtime.version().feature());
System.out.println("Processors at least 1? "
+ (rt.availableProcessors() >= 1));
System.out.println("max >= total >= used? "
+ (rt.maxMemory() >= rt.totalMemory()
&& rt.totalMemory() >= rt.totalMemory() - rt.freeMemory()));
rt.gc();
long before = usedMb(rt);
Integer[] boxed = new Integer[1_000_000];
for (int i = 0; i < boxed.length; i++) {
boxed[i] = i + 1000;
}
rt.gc();
long after = usedMb(rt);
System.out.println("One million Integers used about 15 to 25 MB? "
+ (after - before >= 15 && after - before <= 25));
System.out.println("Kept: " + boxed.length);
}
}Java version: 25
Processors at least 1? true
max >= total >= used? true
One million Integers used about 15 to 25 MB? true
Kept: 1000000Explanation: Runtime.version().feature() gives the major version, 25. Memory is measured as totalMemory() - freeMemory() after requesting a GC, so leftover garbage doesn't count. One million Integer objects use about 20 MB: 16 bytes per object plus 4 bytes per array slot, as explained in "Object Memory Layout in Java". The exact numbers vary, so the program checks a range.
More Detailed Metrics
Common Mistakes
Reading freeMemory() as "memory left before OutOfMemoryError". The heap can still grow up to maxMemory().
Using Runtime.exec with a single command string. It splits on spaces unpredictably; use ProcessBuilder.
Calling halt instead of exit. halt skips shutdown hooks and cleanup.
Practice
- Easy: Print
maxMemory()in MB, then run the program with-Xmx256mand check that it changes. - Harder: Measure the memory used by one million
ints in anint[]and compare it with theInteger[]above.
Key Takeaways
Was this tutorial helpful?