Github storage address: github.com/Damaer/JvmN…
The document address: damaer. Dead simple. IO/JvmNote /
JVM life cycle
- Start the
- perform
- exit
Start the
The Java virtual machine is started by creating an initial class by the Bootstrap Class Loader, which is specified by the implementation of the Java virtual machine.
Custom classes are loaded by the system class loader. The top-level parent of a custom class is Object. As a class in the core API, Object needs to be loaded by the Bootstrap class Loader. The parent class is loaded before the subclass, so the Object class is loaded before the custom one is loaded.
perform
Java
Virtual machine execution has a clear task: executionJava
The program.- The actual program was executed by a guy called
The Java virtual machine
In the process.
exit
The following conditions can be used to exit a VM:
- The program completes normally
- An exception or error occurs during program execution and the program terminates abnormally
- The Java virtual machine process was terminated due to an operating system error. Procedure
- Call by a thread
Runtime
Class orSystem
Of the classexit
Method, orRuntime
Of the classhalt()
Method, andJava
Security manager also allows this operation under conditions. JNI
(java native Interface
) :JNI
的api
Load or unloadJava
Virtual machine,Java
The VM may exit unexpectedly.
System. The exit (), and the Runtime. The halt ()
System.exit() and Runtime.halt():
System.exit() calls the exit() method of the Runtime object. Runtime.getruntime () gets the current Runtime state, which is the Runtime object.
public static void exit(int status) {
Runtime.getRuntime().exit(status);
}
Copy the code
Look at the Runtime exit() method, which calls shutdown.exit (status).
public void exit(int status) {
SecurityManager security = System.getSecurityManager();
if(security ! =null) {
security.checkExit(status);
}
Shutdown.exit(status);
}
Copy the code
If we look at the exit() method of Shutdown, when status is not 0, halt(status) is called.
static void exit(int status) {
boolean runMoreFinalizers = false;
synchronized (lock) {
if(status ! =0) runFinalizersOnExit = false;
switch (state) {
case RUNNING: /* Initiate shutdown */
state = HOOKS;
break;
case HOOKS: /* Stall and halt */
break;
case FINALIZERS:
if(status ! =0) {
/* Halt immediately on nonzero status */
halt(status);
} else {
/* Compatibility with old behavior: * Run more finalizers and then halt */
runMoreFinalizers = runFinalizersOnExit;
}
break; }}if (runMoreFinalizers) {
runAllFinalizers();
halt(status);
}
synchronized (Shutdown.class) {
/* Synchronize on the class object, causing any other thread * that attempts to initiate shutdown to stall indefinitely */sequence(); halt(status); }}Copy the code
Halt (int status) essentially calls a local method halt0(int status) to suspend the virtual machine process and exit.
static void halt(int status) {
synchronized(haltLock) { halt0(status); }}static native void halt0(int status);
Copy the code
The Runtime is an object of Runtime data, a global singleton, which can be understood as representing the run-time data area. Is a hungrier singleton pattern. As you can see from JDK1.0, this is the core class of virtual machines!
You can test the Runtime properties as follows:
public class RuntimeTest {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.getClass().getName());
System.out.println("maxMemory: "+runtime.maxMemory()/1024/1024);
System.out.println("totalMemory: "+runtime.totalMemory()/1024/1024);
System.out.println("freeMemory: "+runtime.freeMemory()/1024/1024); }}Copy the code
Results: indicates that the maximum memory is 2713M, the total memory is 184M, and the available memory is 180M.
java.lang.Runtime
maxMemory: 2713
totalMemory: 184
freeMemory: 180
Copy the code
PS: This note is learned from teacher Song Hongkang’s JVM video, which is based on practice and my own understanding. Address: www.bilibili.com/video/BV1PJ… , highly recommended!!
[Author profile] : Qin Huai, public number [Qin Huai Grocery store] author, the road of technology is not at that time, mountain high water long, even slow, chi and not stop. The world wants everything to be fast and faster, but I hope I can take every step, write every article, and look forward to communicating with you.