Java Virtual Machine Architecture

Source: Internet
Author: User

1, the structure of the JVM :

The JVM consists mainly of the class loader subsystem, the runtime data area (memory space), the execution engine , and the local method interface . The runtime data area is composed of method area, Heap, Java stack, PC register, and local method stack .

It can also be seen that the method area and heap are shared by all Java threads in the memory space, while the Java stack, the local method stack, and the PC register are owned by each thread .

2. Class loader subsystem (class Loader)

The class loader subsystem is responsible for loading the compiled. Class bytecode file and loading the memory so that the JVM can instantiate or otherwise use the loaded class. The class loading subsystem of the JVM supports dynamic loading at runtime, and there are many advantages of dynamic loading, such as saving memory space and flexibly loading classes from the network, another benefit of dynamic loading is that the separation of classes can be achieved through namespace separation, thus enhancing the security of the whole system.

categories of ClassLoader
    • Launch class loader (BootStrap class Loader): Responsible for loading all Java classes in the Rt.jar file, that is, the core class of Java is loaded by that ClassLoader. In Sun jdk, this classloader is implemented by C + + and is not available for reference in the Java language.
    • Extension class loader (Extension class Loader): A jar package that is responsible for loading some extended functionality.
    • System Class Loader: Responsible for loading the jar packages and directories in the classpath specified in the startup parameters, usually the Java classes we write ourselves are also loaded by the ClassLoader. In the Sun JDK, the system ClassLoader is named Appclassloader.
    • user-defined class loader (users Defined class Loader): The load rules for user-defined classes allow you to manually control the steps in the loading process.

How the ClassLoader works

Class loading is divided into three steps: Load, link, and initialize.

(1) Loading

The class is loaded with the fully qualified name and ClassLoader of the class, primarily by loading the specified. class file into the JVM. When the class is loaded, the class is marked with the fully qualified name +classloader instance ID of the class within the JVM.

In memory, instances of ClassLoader instances and classes are located in the heap, and their class information is in the method area.

The loading process employs a method known as the parent delegation model, and when a classloader loads a class, it asks its parent classloader (in fact there are only two ClassLoader, So called the parent ClassLoader may be easier to understand) the load class, and its parent ClassLoader will continue to commit the load request to a higher level of classloader until the class loader is started. Only the parent ClassLoader cannot load the specified class, it will load the class itself.

The parent delegation model is the JVM's first security line of defense, which guarantees the safe loading of classes, which also relies on the principle of class loader isolation: There is no direct interaction between classes loaded by the same loader, and even if the same class is loaded by different classloader, they cannot perceive each other's existence. Thus, even if a malicious class impersonates itself under a core package (for example, Java.lang), it cannot be compromised because it cannot be loaded by the startup ClassLoader.
It can also be seen that if the user customizes the ClassLoader, it is necessary to secure the class loading process.

(2) Links

The task of linking is to merge the binary type information into the JVM runtime state. The link is divided into the following three steps:

    • Validation: Verifies the correctness of the. class file, ensures that the file is compliant with the specification, and is suitable for use by the current JVM.
    • Prepare: Allocates memory for the class, while static variables in the initialization class are assigned the default values.
    • Parsing (optional): The main point is to resolve the symbolic reference in the class's constant pool to a direct reference, which can then be parsed when the appropriate reference is used.

(3) initialization

Initializes a static variable in the class and executes the static code, constructor, in the class.

The JVM specification strictly defines when a class needs to be initialized:

    • When you instantiate an object by using the New keyword, reflection, clone, and deserialization mechanism.
    • When a static method of a class is called.
    • When you use a static field of a class or assign a value to it.
    • When a method of a class is called by reflection.
    • When the subclass of the class is initialized (its parent class must have been initialized before the subclass is initialized).
    • The class that is marked as the startup class when the JVM starts (simply as a class with the Main method).

3. Run-time data area

Java Virtual Machine Architecture

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.