I. Introduction of the JVM
The Java language is cross-platform and compatible with various operating systems. The cornerstone of implementing cross-platform is virtual machine (JVM), virtual machine is not cross-platform, so different OS need to install different JDK version (jre=jvm+ class Library; jdk=jre+ development tool).
1.1. JVM Architecture
Mainly divided into: class loader (ClassLoader) subsystem, runtime data area and execution engine.
- Class Loader: When the JVM starts or the class loads the required classes into the JVM at run time
- Execution Engine: Responsible for executing bytecode instruction in class file, equivalent to CPU
- Run-time data area: divides memory into several zones, accomplishing different tasks separately
1.2. JVM Life cycle
- Startup: When a Java program is started, a JVM instance is generated, and any class that has a public static void main (string[] args) function can be used as a starting point for the JVM instance to run.
- Run: Main () is the starting point for the program's initial thread, and any other threads are started by that thread.
- Extinction: The JVM exits when all non-daemon threads in the program terminate. There are two types of threads in Java: The daemon thread (daemon) and the normal thread (Non-daemon). A daemon thread is a thread that the Java virtual machine uses itself, such as a thread that is responsible for garbage collection, which is a daemon thread. Of course, you can also set your own program as the daemon thread. The initial thread that contains the main () method is not a daemon thread.
One, JVM runtime data area
1.1. Program counter
Thread-private, life-cycle synchronization with threads. The number of lines that the code executes is recorded, and the primary purpose is to restore to the correct location to continue execution when the processor threads are switched. The only area that does not appear outofmemoryerror.
1.2. Virtual Machine stack
Thread-private, life-cycle synchronization with threads. When each method executes, a stack frame is created in the stack for storing local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call to completion, is a stack frame into the stack to the process of the stack.
Local Variables Table: Method-related local variables, including base types (int, float, double, char, BOOL, and so on), object reference (reference), reference address (returnaddress ), and so on.
Two exceptions: If the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, if the virtual machine stack can be dynamically extended (most of the Java virtual machines are now dynamically extensible, but Java A fixed-length virtual machine stack is also allowed in the virtual machine specification, and throws a OutOfMemoryError exception when the extension fails to request enough memory.
1.3. Local Method stack
Thread-private, and the execution of the virtual machine stack is basically the same, the only difference is that the virtual machine stack executes the Java method, the local method stack executes the native method. A OutOfMemoryError exception is thrown when there is not enough memory space.
1.4. Method Area
Thread sharing, storing class information loaded by the virtual machine, constant (final), static variable (static), and immediately compiled code. A OutOfMemoryError exception is thrown when there is not enough memory space.
1.5. Heap
Thread sharing, primarily used to store object instances, the garbage collector's primary area of action. A OutOfMemoryError exception is thrown when there is not enough memory space.
For stack and heap relationships, refer to Java Basics | heap and Stack
1.6. Direct Memory
is not part of the virtual machine run, nor is it an area of memory defined in the Java Virtual Machine specification. A OutOfMemoryError exception is thrown when there is not enough memory space.
Two ways of object access
Understand these two ways in order to deepen understanding of the objects in the stack, heap, method area of the connection.
2.1. Handle Access
The virtual machine stack contains the handle pool address of the object, and the handle pool contains the object's instance address and object type address (class information in the method area);
2.2. Direct pointer access
The virtual machine stack directly points to object instances and object type pointers for faster access.
Third, case analysis
Try to analyze the specific examples, there is a wrong place, ask for guidance.
Packagesrc;Importjava.util.ArrayList;//class information will be stored in the method area Public classPerson {PrivateString name;//stored in the heap because the class is instantiated and stored in the heap, and of course contains its properties Private intAge//stored in the heap Public StaticString Country;//store in method area Public FinalString world = "Earth";//store in method area//when the method is called, a stack frame is created for storing the local variable table in the method, the method exits and other information Public voidGetmessge (string name, String age) {intA = 0;//stored in virtual stacks//arrayList stored in the virtual stack, new arraylist<> () stored in the heapArraylist<string> ArrayList =NewArraylist<>(); }}
Java Basics |java Virtual machine (JVM)