"In-depth understanding of Java Virtual machines: JVM advanced features and best practices" Learning notes Ⅲ virtual machine execution Subsystem

Source: Internet
Author: User

Part III Virtual machine execution subsystem
Chapter 6th class file Structure
The basis of language independence is still the virtual machine and bytecode storage format, using the Java compiler can compile Java code into a storage byte code class file, using JRuby and other languages such as the compiler can compile the program code into a class file, The virtual machine does not care what language the source of the class is, so long as it conforms to the structure of the class file, it can run in the Java Virtual machine.
Structure of the 1.Class class file
A class file is a set of binary streams that are based on 8-bit bytes, and each data item is arranged in a compact order in the class file, with no delimiters in the middle.
The class file format is stored in a pseudo-structure similar to the C-language structure, with only two data types: unsigned number and table.
The first 4 bytes of each class file become magic numbers, and the only purpose is to determine if the file is a class file that can be accepted by the virtual machine.
The primary and secondary version numbers are followed by a constant pool entry, which is the data type most associated with other items in the class file structure. There are two major types of constants, literal (Literal) and symbolic references (symbolic References).

7th Chapter Virtual machine class loading mechanism
The result of code compilation, which is a small step in the development of the programming language, is the transformation from local machine code to bytecode.
The various information described in the class file is loaded into the virtual machine before it can be run and used. And how does a virtual machine load a class file, and what happens after the information in the class file enters the virtual machine?
The virtual machine reads the class file, loads the data of the description class into memory from the class file, verifies the data, transforms parsing and initialization, and then forms a Java type that can be used directly by the virtual machine, that is, the class loading mechanism of the virtual machine.
The language features that are inherently dynamically extensible in//java are based on the feature of dynamic loading and dynamic connection during runtime.
1. Timing of class loading
The class starts from being loaded into the virtual machine memory until it is unloaded out of memory. The entire life cycle consists of seven stages.
Seven phases of loading, validating, preparing, parsing, initializing, using, and uninstalling.
authentication, preparation, and resolution are collectively referred to as connections (linking).
2. The process of class loading
2.1 Load Phase
The load phase, especially the action of getting the binary byte stream of the class at load time, is the most controllable phase of the development period, because the class loader can be used as well as the class loader implementation, and the custom ClassLoader controls how the byte stream is obtained.
2.2 Validation Phase
Ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.
2.3 Preparation Phase
Formally allocates memory for class variables and sets class variable initial values, which are allocated in the method area.
2.4 Parsing Phase
A virtual machine replaces a symbolic reference within a constant pool with a direct reference procedure.
2.5 Initialization Phase
Initialization phase, the Java program code defined in the class is actually started, i.e. bytecode.
3. Class Loader
//class-Loading stage "get a binary byte stream describing this class through the fully qualified name of a class" this action is placed outside the virtual machine implementation,
The code module that implements this action is called the class loader, which is the application's own control acquisition.
Class loaders are applied to the class hierarchy, OSGi, thermal deployment, code encryption, and other fields.
Class 3.1 and Class loader
3.2 Parental delegation Model
From the perspective of a virtual machine, there are two different classloader: one is to start the ClassLoader (Bootstrap ClassLoader), which is implemented in the C + + language, is a part of the virtual machine itself, and the other is all the other class loaders. All inherit from abstract class Java.lang.ClassLoader.
The work of the parent delegation model, where a classloader receives a request for class loading, is first delegated to the parent ClassLoader to complete, all load requests are routed to the top-level startup ClassLoader, and if the parent ClassLoader feedback fails to complete the load request, it continues to be loaded by the subclass.
The benefit of this load is that the Java class has a hierarchical relationship with precedence over its classloader, avoiding the occurrence of a class loaded multiple times.

8th Chapter Virtual Machine bytecode execution engine

1. Runtime stack frame structure
2. Method invocation
3. Stack-based byte-code interpretation execution engine


The case and actual practice of class loading and executing subsystem in the 9th Chapter

In the class file format and execution engine this part, mainly by the virtual machine directly control behavior, can be manipulated by the program, mainly the bytecode generation and class loader two parts.
1. Case studies
Tomcat: The Orthodox class loader structure
Mainstream Java Web servers, such as Tomcat, WebLogic, and so on, implement their own defined ClassLoader.
A fully functional Web server to address the following issues:
Java class libraries that are used by multiple Web applications deployed on the same server can be isolated and shared with each other.

"In-depth understanding of Java Virtual machines: JVM advanced features and best practices" Learning notes Ⅲ virtual machine execution Subsystem

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.