Skip to content

Java Runtime class

The java.lang.Runtime class allows Java applications to interface with the environment in which they are running.^[600-developer__java__java-base__java-Runtime-Number.md] An instance of the Runtime class can be retrieved using the Runtime.getRuntime() method.^[600-developer__java__java-base__java-Runtime-Number.md]

Memory Management

The Runtime class provides several methods for querying the JVM's memory usage.^[600-developer__java__java-base__java-Runtime-Number.md]

  • freeMemory(): Returns the amount of free memory in the Java Virtual Machine.^[600-developer__java__java-base__java-Runtime-Number.md]
  • totalMemory(): Returns the total amount of memory in the Java Virtual Machine.^[600-developer__java__java-base__java-Runtime-Number.md]
  • maxMemory(): Returns the maximum amount of memory that the Java Virtual Machine will attempt to use.^[600-developer__java__java-base__java-Runtime-Number.md]

Additionally, the availableProcessors() method can be used to determine the number of processors available to the JVM.^[600-developer__java__java-base__java-Runtime-Number.md]

Shutdown hooks

The Runtime class allows applications to register threads to be executed upon JVM termination via Shutdown hooks.^[600-developer__java__java-base__java-Runtime-Number.md]

  • Registration: Multiple Shutdown hooks can be added using the addShutdownHook(Thread) method.^[600-developer__java__java-base__java-Runtime-Number.md]
  • Removal: A registered hook can be removed using removeShutdownHook(Thread).^[600-developer__java__java-base__java-Runtime-Number.md]

It is important to note that if the JVM is forcibly terminated using the halt(int) method, Shutdown hooks will not run.^[600-developer__java__java-base__java-Runtime-Number.md]

  • [[Java Virtual Machine]]
  • [[Memory Management]]
  • [[Concurrency]]

Sources

  • 600-developer__java__java-base__java-Runtime-Number.md