Startup
Java virtual machine initialization occurs when the bootstrpa class loader creates an initial class specified by the JVM implementation. Custom classes are loaded by the system class loader, with the core Object class being loaded first by the bootstrap loader due to inhreitance hierarchy requirements.
Execution
During runtime operations, the JVM's primary function is executing Java applications within a dedicated process environment.
Termination
JVM shutdown occurs under these conditions:
- Normal program completion
- Unhandled exceptions or errors
- Operating system failures
- Invocation of
System.exit()orRuntime.halt()with security manager approval - Native interface (JNI) operations causing abnormal termination
Termination Mechanisms
System.exit() delegates to Runtime.exit():
public static void exit(int status) {
Runtime.getRuntime().exit(status);
}
Runtime.exit() triggers shutdown procedures:
public void exit(int status) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(status);
}
Shutdown.terminate(status);
}
The shutodwn handler invokes native termination:
static void terminate(int stateCode) {
// Shutdown sequence logic
if (stateCode != 0) {
immediateHalt(stateCode);
}
}
static void immediateHalt(int code) {
synchronized (lock) {
nativeHalt(code);
}
}
static native void nativeHalt(int code);
Runtime is a singleton representing the execution environment. This example demonstrates memory metrics:
public class RuntimeMetrics {
public static void main(String[] args) {
Runtime env = Runtime.getRuntime();
System.out.println(env.getClass().getName());
System.out.println("Maximum Memory (MB): " + env.maxMemory() / (1024 * 1024));
System.out.println("Allocated Memory (MB): " + env.totalMemory() / (1024 * 1024));
System.out.println("Available Memory (MB): " + env.freeMemory() / (1024 * 1024));
}
}
Example output:
java.lang.Runtime
Maximum Memory (MB): 2713
Allocated Memory (MB): 184
Available Memory (MB): 180