JAVA virtual machine class loading mechanism and byte code execution engine

Source: Internet
Author: User
Tags abstract tree field table

Introduction

We know that Java code is compiled with bytecode, how does a virtual machine load these class bytecode files? How is the method call made after loading?


A class of file structuresThe cornerstone of Independence

Java has a slogan called Write once, running around. The implementation of this slogan is a virtual machine that can run on a different platform and a platform-independent byte code. It is important to note that the virtual machine is also neutral, as long as the code is compliant with the bytecode, can be accepted by the virtual machine, such as Groovy,jruby and other languages, will generate code-compliant bytecode, and then run by the virtual machine, the virtual machine does not care about the language generated by the bytecode.

class file Structure

Class files are a set of 8-byte-based binary streams that contain the following sections:

Magic number and class file version: The four bytes at the beginning of the class file are defined as Cafebabe, only the file that starts with Cafebabe can be accepted by the virtual machine, the next four bytes are the version number of the class file, and the high version JDK can be compatible with the previous version of the class file. However, you cannot run a later version of the class file.

Chang: It can be understood as a repository in a class file that contains two major classes of constants: literal and symbolic references, literal literals containing literal strings, constant values declared final, symbolic references containing fully qualified names of classes and interfaces, names and descriptors of fields, names and descriptors of methods.

Access flag: After the constant pool ends, two bytes are followed by an access flag that identifies the access information for classes or interface hierarchies, such as whether public, static, and so on.

Class index, parent class index, and Interface index collection: The class index is used to determine the fully qualified name of the class, the parent class is the fully qualified name of the parent class, and the interface index collection is the fully qualified name of the interface.

Field table collection: Used to describe an interface or a variable declared in a class, but does not contain a variable in a method.

Method table collection: Used to describe a method in an interface or class.

Property sheet Collections: Class files, field tables, and properties from the method table are all derived here.


Two-class loading mechanism

The virtual machine loads the data of the description class from the class file into memory, verifies the data, transforms the analysis and initialization, and finally forms the Java type that can be used directly by the virtual section, which is the class loading mechanism of the virtual machine.

Classes from being loaded into virtual machine memory to unloading out-of-memory life cycle include: initialization---------------

5 Types of initialization scenarios:
    1. When instantiating an object by using the New keyword, read or set a static field for a class, except when the static method of the class is called by the final adornment compiled result placed in a constant pool.

    2. When you use the Java.lang.reflect package method to make a reflection call to a class. (Class.forName ()).

    3. Initializes the child class if the parent class is not initialized.

    4. The class that the main method resides on when the virtual machine starts.

    5. When using JDK1.7 Dynamic language support, the Java.lang.invoke.MethodHandle instance resolves the result to a method handle of ref_getstatic,ref_putstatic,ref_invokestatic, and the corresponding class is not initialized.

Class loading process

Load

Loading is the first stage of class loading, and the virtual machines complete the following three procedures: 1 gets the binary byte stream that defines this class through the fully qualified name of the class. 2) Convert the storage structure of the byte stream into the run-time structure of the method area. 3) Generate a class object in memory that represents the category as a access entry for various data in the method area.

Verify

The purpose is to ensure that the class file byte stream information conforms to the requirements of the virtual machine.

Get ready

Assign an initial value to a static modified variable, such as the default of type int to 0,boolean defaults to false.

Analytical

A virtual machine replaces a symbolic reference in a constant pool with a direct reference.

Initialization

Initialization is the last phase of class loading, and the class constructor <init> () method is executed, noting that the method here is not a construction method. The method will explicitly call the parent class constructor and then assign a value to the class variable and the static statement block in the Java statement order.

Class Loader

For any class, it is necessary to establish its uniqueness in the Java Virtual machine, together with the class loader that loads it and the class itself. To give an example:

package com.sinaapp.gavinzhang.bean;import java.io.inputstream;public class app {     public static void main ( String[] args )      {        classloader myclassloader = new  classloader ()  {             @Override             public Class<?>  LoadClass (String name)  throws ClassNotFoundException {                 try{                     string filename =  name.substring (Name.lastindexof (".") +1) + ". Class";                &nbsP;    inputstream is = getclass (). getResourceAsStream (FileName);                      if (is == null)                      {                         system.out.println (fileName+   "Is not find");                         return super.loadclass (name);                      }                     system.out.priNtln ("filename: " +filename);                     byte[] b = new byte[is.available ()];                      is.read (b);                     return defineclass (name,b,0,b.length);                 }catch  (exception e)                  {                     throw new  ClassCastException (name);                 }            }        } ;        try {             object obj = myclassloader.loadclass (" Com.sinaapp.gavinzhang.bean.Resource "). Newinstance ();             object obj1  = class.forname ("Com.sinaapp.gavinzhang.bean.Resource"). Newinstance ();             system.out.println (obj  instanceof com.sinaapp.gavinzhang.wesound.bean.resource);             system.out.println (obj1 instanceof  Com.sinaapp.gavinzhang.wesound.bean.Resource);        }catch  ( Exception e) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;       e.printstacktrace ();         }    }}

The result is:

As you can see, a custom load class can only get the class under the same package, and the class of the system cannot be loaded, and the classes obtained by Class.forName () are not the same class as the custom load class.

The parent class will also be initialized based on the five initialization conditions, but the result of the code running above shows that neither the parent class nor the interface is initialized, and what is going on?

The system provides three kinds of loaders, which are: Start the ClassLoader (Bootstrap ClassLoader), which loads the classes in the <java_home>\lib directory that can be recognized by the virtual machine into memory. Extension class loader (Extension ClassLoader), which loads the class library in the <java_home>\lib\ext directory into memory. The Application class loader (application ClassLoader), which is responsible for loading the class library specified on the user path.

Our custom ClassLoader inherits from the application ClassLoader, and when the custom ClassLoader does not find the class that is added, it is loaded using the startup ClassLoader, which is loaded by the extension class when the startup ClassLoader is not loaded, and when the extension class is loaded without the application class. This is why the above code can run successfully.


Three-byte code execution engineRun-time stack frame structure

http://my.oschina.net/jiangmitiao/blog/470426 that the virtual machine stack is thread-private, the thread creates a stack frame for the running method.

Stack frame is the stack element of the virtual machine stack, the stack frame stores the local variable table, the operand stack, the dynamic connection, the return address and other information. Each method's call corresponds to a stack frame in the virtual machine stack in the stack and out of the stack.

A local variable table consists of a method parameter, a local variable defined within the method, and the capacity in the variable slot (slot) as the smallest unit. If the method is not a static method, the first index of the local variable table is a reference to that object, which can be taken with this.

The operand stack is initially empty, the data is stored in the stack by the bytecode instruction, and the return value of the method is stored in the operand stack of the previous method.

A dynamic connection contains a reference to the method that the stack frame belongs to in the constant pool, which is held for dynamic dispatch.

The method return address holds the PC counter value that calls the method, and when the method returns normally, it passes the return value to the upper-level method caller. When an exception occurs in a method that is not caught, it is returned, but the return value is not passed up the layer.

Method invocation

Java is an object-oriented language that is polymorphic. So how does a virtual machine know which method to call at runtime?

Static dispatch is a typical static dispatch that determines which method to call, rather than the virtual machine, at compile time.

Dynamic dispatch is the time at which a virtual machine runs to decide which method to call, and a method override is a typical dynamic dispatch.

The implementation of a dynamic dispatch: When a method of an object is called, the reference to the object is pushed to the operand stack, and then the bytecode directive invokevirtual to look for the actual type of the reference. If the corresponding method is found in the actual type, and the access permission is sufficient, the method reference is returned directly, otherwise the parent class will be looked up according to the inheritance relationship. In fact, if the subclass does not override the parent class method, the reference to the subclass method directly points to the parent class method.


Stack-based byte-code execution engine

The machine cannot understand the non-binary language, whether it is an interpreted or a compiled language. High-level language translates into machine language followed by modern classic compilation principles. That is, the program source code before execution lexical and grammatical analysis, the construction of abstract syntax tree. Compiled languages such as the C language are performed by a separate execution engine, while an abstract tree of interpreted language syntax, such as the Java language, is done by the JVM. The JVM can choose to interpret bytecode execution through an interpreter or to generate machine code from the optimizer.

The two common sets of instruction set schemas are stack-based instruction sets and register-based instruction sets respectively.

Stack-based instruction sets are added to the stack to achieve the calculation function, such as

Iconst_1 the 1 into the stack iconst_1, the 1 into the stack iadd, the top two elements of the stack out and add the results into the stack

Register-based instruction sets are more about using registers to operate, such as

MOV eax,1; save 1 Add eax,1 to eax; eax<-eax+1

Overall, the stack-based instruction set is slower, but it is not related to registers and makes it easier to run anywhere.


Summarize

It's time to summarize, and it's easy to be asked in the class loading mechanism interview, unfortunately, I didn't see that knowledge at the time.

Each part of the class file structure can be further down, and the class file structure is stored in a structured way, so how to know the length of the collection, and how each property is tagged.

There are only five scenarios in the class loading mechanism that trigger initialization. The class loader classification.

The structure of the stack frame, and the method call.

The method invocation of the Java language is divided into static multi-Dispatch, Dynamic single dispatch.

JAVA virtual machine class loading mechanism and byte code execution engine

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.