Java virtual machine architecture
Since my work, the more code I write, the more bloated the program, and the lower the efficiency. For a programmer who pursues perfection, this is absolutely not allowed, as a result, in addition to constantly optimizing the program structure, memory optimization and Performance Tuning have become my "tricks ".
To optimize the memory and performance of Java programs, it is certainly not feasible to understand the internal principles of virtual machines (or to call specifications more rigorous, here we recommend a good book "deep into Java Virtual Machine (second edition)" (translated by Bill Venners, translated by Cao Xiaogang Jiang Jing. In fact, this article is the author's personal understanding of Java virtual machine after reading this book ). Of course, understanding the advantages of Java virtual machines is not limited to the above two benefits. From a deeper technical perspective, understanding the specifications and Implementation of Java virtual machines will help us write efficient and stable Java code. For example, if we understand the memory model of the Java Virtual Machine and the memory collection mechanism of the virtual machine, we will not rely too much on it, the memory will be "released" explicitly as needed (Java code cannot explicitly release the memory, but it can notify the Garbage Collector to recycle the object by releasing the object reference ), to reduce unnecessary memory consumption. If we understand the working principle of Java stack, we can reduce the risk of Stack Overflow by reducing the number of recursive layers and reducing the number of cycles. It may not directly involve the underlying implementation of these Java virtual machines for application developers. However, understanding these background knowledge may have a subtle influence on the programs we write.
This article will briefly describe the architecture and Memory Model of the Java Virtual Machine. If the useful words are incorrect or the explanations are inaccurate, please kindly correct them!
Java virtual machine architecture
Class loading Subsystem
The Java Virtual Machine has two types of loaders: the startup class loader and the User-Defined loader.
The Class loading subsystem uses the fully qualified Class package name and Class Name of the Class, and the network load also includes the URL) to load the Class into the runtime data zone. For each mounted type, the java Virtual Machine creates a java. lang. class instances represent this type. This instance is placed in the heap area of the memory, and the loaded type information is located in the method area, which is the same as all other objects.
Before loading a type, the class loading subsystem should not only locate and import the corresponding binary class file, but also verify the correctness of the import class, allocate class variables and initialize the memory, and parse the symbolic reference as direct reference. These actions are performed in the following order:
1) load -- Query and load binary data of the type;
2) connection -- perform verification, preparation, and resolution (optional)
3) verify the correctness of the imported type
4) Prepare to allocate memory for the class variable and initialize it as the default value.
5) Parse and convert the symbolic references in the type into direct applications
Method Area
For each type Loaded by the class loading subsystem, the virtual machine will save the following data to the Method Area:
-
Type Full qualified name
-
Fully qualified names of Type superclasses (java. lang. Object does not have superclasses)
-
Type: class type or interface type
-
Type of access modifier
-
List of fully qualified names of any direct superinterface
In addition to the above basic type information, the following information will be saved:
-
Type constant pool
-
Field Information (including field name, field type, field modifier)
-
Method information (including method name, return type, number and type of parameters, method modifier, if the method is not abstract or local, the size and exception table of the bytecode, operand stack, and local variable area in the method stack frame will also be saved)
-
All other class variables except constants (in fact, static variables of the class, because static variables are shared by all instances and directly related to the type, they are class-level variables, stored as a class member in the Method Area)
A reference to class ClassLoader
// The returned result is the ClassLoader reference saved just now.
String. class. getClassLoader ();
One to Class reference
// Returns the reference of the Class just saved.
String. class;
Note that the method area can also be recycled by the garbage collector.
Heap
All class instances or arrays created during Java program running are stored in the same heap, and each Java virtual machine also has a pair of space, all threads share a heap (this is why a multi-threaded Java program will cause synchronization of Object Access ).
Because each type of Java Virtual Machine has different implementations of the virtual machine specification, we may not know how each type of Java Virtual Machine represents an object instance in the heap, however, we can take a look at the following possible implementations:
Program counters
For a running Java program, each thread has its own PC program counter) Register, which is created at the startup of the thread and is a wordlength, used to save the location of the next line of code to be executed.
Java stack
Each thread has a Java stack that stores the running status of the thread in the unit of stack frames. The virtual machine has two types of operations on the Java stack: The pressure stack and the out stack, both of which are frame-based. The stack frame stores data such as input parameters, local variables, and intermediate calculation results. When the method is completed, it is popped up and then released.
Take a look at the memory snapshot of the stack frame when the two local variables are added.
Local method Stack
This is where Java calls the local library of the operating system and is used to implement JNIJava Native Interface and Java Local Interface)
Execution engine
The core of the Java Virtual Machine is to load and parse the Java bytecode. For running Java programs, every thread is an instance of an independent virtual machine execution engine, from the beginning to the end of the thread life cycle, he is either executing the bytecode or executing the local method.
Local interface
Connects the local method stack and the operating system library.
Note: All "Java virtual machines" mentioned in this Article refer to "Java Virtual Machine specifications for Java EE and JavaSE platforms ".