[Reading Notes] Deep Java Virtual Machine-Java architecture (2)

Source: Internet
Author: User

Three Meanings of Java virtual machine:
-Abstract specifications
-A specific implementation
-A running Virtual Machine instance

------------------- Life cycle of the Java Virtual Machine:
A Java Virtual Machine instance is responsible for running a Java program.
When a Java program is started, a virtual machine instance is born. When the program is closed and exited, the virtual machine disappears.
There are several Java programs running, and there are several Java Virtual Machine instances. Each Java program runs in its own Java Virtual Machine instance.

Java Virtual Machine has two threads:
-Daemon thread: it is used by the virtual machine itself (the garbage collection thread is executed). Java programs can also mark any thread as a daemon thread.
-Non-daemon thread: Main.

When non-daemon threads are terminated, the VM instance automatically exits. (You can also call the exit () method of runtime or system to exit the program ).

--------------------- Architecture of the Java Virtual Machine:
See chapter 5 of the same directory. Java Virtual Machine-architecture diagram!
The method zone and heap are shared by all threads of the Virtual Machine instance.
When a virtual machine loads a class file, it will parse the type information from the binary data contained in the class file, and then put the type information into the method area.
When the program is running, the virtual opportunity puts all the objects created during the program running in the heap.

When a new thread is created, it will get its own PC register and a java stack (not all threads share ).
The Java virtual machine creates a memory area for each thread. These memory areas are private and no thread can access the PC register or java stack of another thread.
If the thread is executing a Java method (non-local method), the value of the PC register always indicates the next instruction to be executed.
The java stack always stores the status of Java method calls in this thread (local variables, parameters passed in When called, return values, intermediate results of operations)

The local Method Invocation status is stored in the local method stack in a way that depends on the specific implementation, or in registers or some memory zones related to the specific implementation.

--------------------- Stack frame (1 ):
A java stack consists of many stack frames. A stack frame contains the status of a Java method call.
When a thread calls a Java method, the VM pushes a new stack frame to the java stack of the thread. When the method returns, the stack frame is popped up and discarded from the java stack.

Java virtual machines do not have registers, and their instruction sets use Java stacks to store intermediate data (for platform independence ).

--------------------- Type data
Data types are divided into the following:
Basic types: float, double, byte, short, Int, long, Char, and Boolean
Reference Type: Reference (class reference, interface reference, array reference)
Note: Boolean is represented by INT when compiled into bytecode,

In a Java virtual machine, the most basic data unit is the word, with at least 32 characters as the word length.
Most of the content in the Data zone during running is based on the abstract concept of "word.

--------------------- Class Loader subsystem:
Loading, connection, and initialization of the Class Loader:
-Load: searches for and loads binary data of the type.
-Connection: performs verification, preparation, and resolution (optional ).
Verify: ensure the correctness of the imported type.
Preparation: allocate memory for static variables and initialize it to the default value.
Resolution: converts a symbolic reference in a type to a direct reference.
-Initialization: Initialize static variables to the correct initial values.

Start the Class Loader:
As long as it is a binary file that complies with the Java class file format, Java Virtual Machine implementation must be able to identify and load the class and interface in the device. Each Java Virtual Machine implementation must have a startup class loader to load trusted classes, such as Java APIs.

User-Defined Class Loader:
The user-defined class loader is a common Java object. Its class must be derived from the java. Lang. classloader class. The methods defined in the classloader class provide interfaces for the program's class loader mechanism.
The user-defined class loaders and class instances are all placed in the heap area of the memory, while the loaded type information is located in the method area.

Four Methods of classloader class to Java virtual machine:
Protected final class defineclass (string name, byte data [], int offset, int length );
Receives a byte array named data, in addition, the binary data between data [offset] and data [Data + Length] must comply with the Java class format (indicating a new available class). Name is the fully qualified name of this type, the default protection domain is assigned. Note: When this method returns a class object instance, it indicates that the specified class file has been found and loaded to the Method Area (not necessarily connected and initialized)

Protected final class defineclass (string name, byte data [], int offset, int length,
Protectiondomain );
As before, this type of protection domain will be specified by its protectiondomain parameter.

Protected final class findsystemclass (string name );
Receives a string name (a fully qualified name of this type), which is used to start the system class loader (which must be enabled ).

Protected final void resolveclass (Class C );
Receives a reference from a class instance as a parameter. It performs a connection action on the Type indicated by the class instance (the connection action must be executed ).

--------------------- Method Area:
All threads share the method area, so their access to the method area data must be designed to be thread-safe.
The size of the method area is not fixed, and the virtual machine can be dynamically adjusted according to application needs.
The method area does not need to be continuous.
The method area can also be garbage collected.

Type information:
For each load type, the virtual machine stores the type information in the Method Area:
-Full qualified names of this type
-Full qualified names of direct superclasses of this type (unless it is an object class, there is no superclass)
-Is this type A class type or an interface type?
-This type of access modifier (a subset of public, abstract, and final)
-An ordered list of fully qualified names for any type of direct superinterface

-Constant pool of this type [core]: Symbol reference of all constant type field methods (index access)
-Field Information: field name, field type, field Modifier
-Method information: method name, return type, parameter quantity and type (in order), method Modifier
-Static variables except Constants
-A reference to class classloader: tracks the class of the loader used to save the class loading.
-One reference to the class: a virtual opportunity is used to create an instance of the corresponding java. Lang. Class class.

--------------------- Method table:
Method table: Type Data Structure
The Virtual Machine generates a table for each non-abstract class and stores it as a part of the class information in the method area.
The method table is an array, and the element is a direct reference to all instance methods that may be called by its instances.
This includes methods directly inherited by superclasses. You can use the method table to quickly search for instance methods called in objects during running.

--------------------- Heap:
All class instances or arrays created during Java program running are stored in the same heap (thread synchronization should be considered ).
Like the method area, the heap space is not a continuous memory area.

--------------------- Array:
Internal representation of the array:
In Java, arrays are real objects.
Like other objects, arrays are always stored in the heap.
Like other objects, arrays also have a class instance associated with their classes, all arrays with the same dimension and type are instances of the same class (regardless of the length of the array ).

The name of an array class consists of two parts. Each dimension is represented by a "[", and the element type is represented by a character or string. For example, "[[ljava/lang/object" indicates that the element is a two-dimensional array of the object.

In the heap, each array object must also store the length of the array, array data, and some reference class data pointing to the array.

------------------- Program counter (PC register ):
Each thread has its own PC register, which was created when the thread was started.
The size of the PC register is the font length, so it can hold a local pointer and a returnaddress. When a thread executes a Java method, the content of the PC register always writes an address of the command to be executed. The address here can be a local pointer, it can also be the offset relative to the actual instruction of the method in the method bytecode. If the thread is executing a local method, the PC register value is "undefined ".

--------------------- Java stack:
Each time a new thread is started, the Java Virtual Machine assigns a java stack to it.
Virtual Machine Intelligence directly performs two operations on the java stack: frame-based pressure stack or out stack.
Every time a thread calls a Java method, the VM will press a new frame in the java stack.
Whether the Java method returns normally or throws an exception, the VM will pop up the current stack java stack and then release the memory.

Java stacks and stack frames do not need to be consecutive in the memory.

------------------- Stack frame (2 ):
Stack frame composition:
-Local Variable Area:
Is an array that starts counting from 0 in the unit of word length. The bytecode command uses the data in the index starting from 0.
Int, float, reference, and returnaddress values only occupy one item in the array;
The values of byte, short, and Char are converted to int before being saved to the array, so only one item is occupied;
Boolean virtual machines are not supported and are represented by Int. Therefore, only one item is occupied;
The values of long and double represent two items in the array (if you specify the first index value ).
The local variable area contains the corresponding method parameters and local variables.
Store the data in the local variable array in the order of parameters. The real order of local variables is arbitrary.

-Operand Stack:
It is an array in bytes (not accessed through indexes) and accessed through standard stack operations (pressure stack and out stack.
The stored data type is the same as that in the local variable area.
The virtual machine uses the operand stack as its workspace. Most commands need to pop up data from here, execute the calculation, and then press the result back to the operand stack.

-Frame data zone:
Constant pool resolution, normal method return, and exception dispatch.
Every time a VM executes a command that requires constant pool data, it accesses it through a pointer pointing to the constant pool in the frame data area.
The frame data area also helps the virtual machine process the normal termination or exceptional suspension of the Java method:
If the call ends normally: the virtual machine must resume the stack frame of the method (the previous one) that initiates the call, And the next instruction of the call completion method instruction. If a return value exists, the virtual machine will overwhelm the return value.

The operand stack of the call method.
If an exception is thrown, it is handled based on the exception table in the frame data area. If a catch statement exists, it is handed over to the Code in the catch statement. If no, an exception is aborted immediately.

--------------------- Local method Stack:
When a thread calls a local method, the virtual opportunity remains unchanged in the java stack, and no new frames are added to the java stack of the thread. The virtual machine simply connects dynamically and calls the specified local method directly.

Just like other runtime memory zones, the memory occupied by the local method stack does not have to be fixed. It can be dynamically expanded or reduced as needed.

--------------------- Execution engine:
The core implementation of any Java Virtual Machine is its execution engine.
In Java Virtual Machine specifications, execution engine behaviors are defined using instruction sets.
As a running instance, the execution engine is a thread.
Every thread of the running Java program is an instance of an independent virtual machine execution engine. From the beginning to the end of the lifecycle, it either executes the bytecode or executes the local method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.