Week 23 — What is the Java's `Runtime` class and what does it do?
Question of the Week #23
What is the Java's
Runtime
class and what does it do?5 Replies
The Runtime class in Java is an object that allows you to launch new processes, work with system resources and manage the execution process of a program. This class has methods to interact with the operating system, such as executing external commands, terminating the JVM process, and determining the maximum amount of memory available for your application.
For example, you can use Runtime to retrieve the maximum amount of free memory that JVM can allocate, or to check the total amount of available memory at the moment.
Here is a sample code using the Runtime class:
Submission from ЯЖЕГУСЪ#7761
Each Java application has a single instance of the Runtime class. This allows the application to interact with the environment in which the application is running. The
getRuntime
method determines the current Runtime. An application cannot create its own instance of this class.
For example, you can use the runtime class to read out the total and free memory:
Runtime.getRuntime().totalMemory();
Runtime.getRuntime().freeMemory();
⭐ Submission from StormOfGalaxy#8898
The Runtime class provides util, that allows the developer to inspect the current running VM. Things like memory stats, the amount of cores of the cpu, etc are found there.
Runtime
itself is nonstatic - all the methods are bound to an instance. You can't just make a new instance of it tho, there's only one instance available, stored in Runtime.currentRuntime
. You can obtain it via Runtime.getRuntime()
There is one static method on it - Runtime.version()
. It provides version information about the current VM, things like the build number and semver information.
Other funnies of the runtime class include
- exec()
: executes a shell command
- addShutdownHook()
: Adds a thread that runs when the jvm shuts down
- gc()
: Runs the garbage collector
- and more
The class itself isn't that big, but it does provide some nice tools to manipulate and work with the jvm runtime.⭐ Submission from 0x150#9699
The class
java.lang.Runtime
provides multiple methods for interacting with the JVM.
One can get an instance of the Runtime
class using Runtime.getRuntime()
.
For example, it is possible to use Runtime.availableProcessors
in order to get the number of processors usable by the JVM. It is also possible to get information about memory usage using Runtime#freeMemory
and Runtime#totalMemory
or access the currently used Java version with Runtime#version
.
Aside from obtaining system information, the Runtime
class can also be used to execute system command using Runtime#exec
:
Furthermore, it allows registering (and removing) shutdown hooks. Shutdown hooks are executed when the JVM shuts down gracefully.
Another method provided by the Runtime
class is called halt
. This method immediately stops the JVM. Shutdown hooks or similar are not executed when calling Runtime#halt
.
(answer by me, wasn't reviewed/no point was granted - I'll just leave that here for archiving purposes)