Java Reference ยท java.lang
The Runtime class
Reference for java.lang.Runtime: processor count, memory figures, shutdown hooks, version information, gc hints and why exec is replaced by ProcessBuilder.
Every running Java program has exactly one Runtime object that represents the JVM it runs in. You never construct it; you obtain it with Runtime.getRuntime(). It reports how many processors and how much memory are available, lets you register code to run at shutdown, and exposes version information.
Most of what it offers is for diagnostics and tuning. Treat memory numbers as rough indicators, not exact measurements.
Runtime rt = Runtime.getRuntime(); int cpus = rt.availableProcessors(); long maxHeap = rt.maxMemory();
Try it
Memory values differ from machine to machine, so the examples print relationships rather than raw numbers.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class Runtime
| Member | Description |
|---|---|
static Runtime getRuntime() |
The single instance. |
int availableProcessors() |
Number of processors the JVM may use. Respects container limits. May change during the run. |
long maxMemory() |
Maximum heap the JVM will try to use (-Xmx). |
long totalMemory() |
Heap currently reserved by the JVM. |
long freeMemory() |
Unused part of the currently reserved heap. |
void gc() |
A hint to collect garbage. |
void addShutdownHook(Thread) / boolean removeShutdownHook(Thread) |
Registers or removes a not-yet-started thread to run when the JVM shuts down. |
void exit(int) / void halt(int) |
Orderly or immediate termination; see System.exit(). |
static Runtime.Version version() |
Structured version of the running Java (since Java 9). |
Process exec(...) |
Starts an external program. Deprecated since Java 18 in favor of ProcessBuilder. |
Usage notes
- Memory arithmetic. Used memory is
totalMemory() - freeMemory(); free memory the program can still claim is closer tomaxMemory() - used. The numbers fluctuate with garbage collection, so they are good for logs, not for decisions. - Sizing thread pools.
availableProcessors()is the usual starting point for CPU-bound pools. In containers, modern JVMs honor CPU limits, so the number can be lower than the host's core count. - Shutdown hooks run on normal exit,
System.exit, Ctrl+C andSIGTERM, but not afterSIGKILL,halt()or a crash. Registering the same thread twice throwsIllegalArgumentException, and registering during shutdown throwsIllegalStateException. - Hooks run concurrently and should be quick, thread-safe, and avoid depending on other services that may already be shutting down.
gc()is only a suggestion; do not call it in production code.- Use
ProcessBuilderto start programs; it handles arguments, working directory, environment and stream redirection explicitly. runFinalization()and the finalization mechanism are deprecated for removal.- Memory-mapped and off-heap memory (direct buffers, native code) is not counted in these numbers.