JVM Architecture-Working principle of __JVM

Source: Internet
Author: User

Original address: How JVM WORKS–JVM architecture?

The JVM (Java Virtual Machine) is the Run-time engine (Run-time engine) that is used to run the Java application. The JVM is the main method invoked in Java code. The JVM is part of the JRE (Java Run environment).

Java applications are called "once written, run everywhere." This means that code farmers can write Java code on a machine that can be run in any other Java embedded system without any additional formalities. You can do this because there's a JVM.

When we compile a A.java file, the Java compiler generates a a.class file with the same name (including bytecode). When we run this. class file, it goes through several stages. These phases together describe the entire JVM.

Class loading Subsystem

It is mainly responsible for 3 kinds of activities: loading connection initialization

Load : The class loader reads the. class file, generates the corresponding binary data, and saves it in the method area. For each. class file, the JVM stores the following information. The full name of the loaded class and its direct parent class, and whether the. class file is related to a class, interface, or enumeration, modifier, variable, and method information, and so on.

When the. class file is loaded, the JVM creates an object of this class type in the heap store to represent the file. Please note: The object of this class has been defined in advance in the Java.lang package. Code farmers can use objects of this class by acquiring class-level information, such as class names, methods, and variable information. We can use the GetClass () method of the object class to get a reference to the object.

A Java program to demonstrate working of the A Class type//object created by JVM to represent. Class file in//memory.
Import Java.lang.reflect.Field;

Import Java.lang.reflect.Method;  Java code to demonstrate with Class object//created by JVM public Class Test {public static void main (string[)

        args) {Student S1 = new Student ();
        Getting hold of Class object created//by JVM.

        Class C1 = S1.getclass ();
        Printing type of object using C1.

        System.out.println (C1.getname ());
        Getting all methods in a array method m[] = C1.getdeclaredmethods ();

        For (method Method:m) System.out.println (Method.getname ());
        Getting all fields in an array Field f[] = C1.getdeclaredfields ();
    for (Field field:f) System.out.println (Field.getname ());
The//A sample class whose information is fetched above using//It class object. Class Student {PrivaTe String name;

    private int roll_no;   Public String GetName () {return name;
    public void SetName (String name) {this.name = name;}  public int getroll_no () {return roll_no;
    The public void setroll_no (int roll_no) {this.roll_no = Roll_no; }
}

Output:

Student
getName
setname
getroll_no
setroll_no
name
roll_no

Note : Only one object of this class can be created for each loaded. class file.

Student s2 = new Student ();
C2 'll point to same object where 
//C1 is pointing
Class c2 = S2.getclass ();
System.out.println (C1==C2); True

Connectivity : Performance verification, preparation, parsing (optional). Validation: Ensure that the. class file is correct. For example, check to see if this file is properly formatted and generated by a valid compiler. If the validation fails, then we'll get to the java.lang.VerifyError exception. Preparation: The JVM allocates memory for variables of the class and initializes them with default values. Parsing: This is the process of replacing a direct reference of this type with a symbolic reference. Locate the referencing entity by querying in the method area.

Initializes : In this section, all static variables are assigned with values defined in the code and static blocks, if any. This execution occurs from top to bottom, from the parent class of a class to the subclass that inherits the parent class. In general, there are three kinds of loaders: the Bootstrap class loader: Each JVM implementation must have a bootstrap class loader that can load trusted classes. It loads the core Java API under the directory java_home/jre/lib. This path is also a well-known bootstrap path, which is implemented in languages such as C or C + +. Extended class Loader (Extension class loader): It is the child of the bootstrap ClassLoader, which loads the extended path java_home/jre/lib/ The class in Ext or the other path specified by the Java.ext.dirs system property. It is implemented using the Sun.misc.launcher$extclassloader class with Java. System/Application class loader (System/application class loader): It is the child of the extended ClassLoader, primarily responsible for loading classes from the application classpath. The environment variables used within it are mapped to Java.class.path, which is also implemented in the Java language through the Sun.misc.launcher$extclassloader class.

Java code to demonstrate Class Loader subsystem public
class Test {public
    static void Main (string[] args) {
  //String class is loaded by bootstrap loader, and
        //bootstrap loader are not Java object, hence null
        System.out . println (String.class.getClassLoader ());

        Test class is loaded by Application loader
        System.out.println (Test.class.getClassLoader ());
    }
   

Output:

Null
sun.misc.launcher$appclassloader@73d16e93

Note : The JVM follows the delegation-hierarchy principle to load classes. The System class loader delegates the load request to the extended class loader, and the extended class loader then delegates the request to the Boot-strap class loader. If the class is found under the Boot-strap path then the class is loaded, otherwise the request is transferred to the Extended class loader and then to the System class loader. Finally, if the System class loader is not loaded into the class, the runtime exception is obtained: Java.lang.ClassNotFoundException.

JVM Memory

Method Area: In the method area, all class-level information, such as class name, direct parent class name, method, and variable information, are all saved, including static variables. There is and only one method area in each JVM, and it is a shared resource.

Heap Area : The heap area stores information for all objects. There is also only one heap area in each JVM, and it is also a shared resource.

Stack area : For each thread, the JVM creates a run-time stack and saves the thread here. Each piece of this stack is called the active record/stack frame, where the invocation of the method is saved. All local variables of the method are saved in the relevant frame. When a thread is finished, the JVM destroys the runtime stack. It is not a shared resource.

Program Counter Register (PC registers): Saves the address of a thread's current execution instruction. It is clear that each thread has a separate program counter register.

Local methods Stack (Native method Stacks): For each thread to establish a separate method stack, it holds the information of the local method.

Execution Engine

The execution engine executes A. Class (bytecode) file. It reads the bytecode line by row, using data and information in the variable store to represent and execute instructions. It can be divided into three parts: interpreter: It interprets the bytecode line by row and then executes. Here is a disadvantage, when a method is called multiple times, each interpreter is executed. Just-in-time Compiler (Just-In-Time Compiler (JIT)): It is used to prompt the interpreter for efficiency, it compiles the entire bytecode and converts it to local code, so whenever the interpreter sees a duplicate method call, JIT provides direct local code. This does not need to be explained again, so the efficiency is improved. Garbage collection: It destroys objects that are not referenced. For more information, please refer to garbage collection. Java Local Interface (JNI)

It is an interface that interacts with the local method library and provides a local library (c, C + +) to execute. It makes it possible for the JVM to call the C + + library, or it can be called by the C/s library already specified by the hardware. Local Method Library

It is a collection of local libraries (c/s) required by an execution engine,

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.