Quickly understand the principle of JVM - [Basic Concepts]

Quickly understand the principle of JVM - [Basic Concepts]

What is a Java virtual machine?

The essence of the Java virtual machine is a " virtual " computer. It is generally understood that a "software" is installed on the computer.

Virtual machines generally fall into two categories:

  • System virtual machine [software platform that can run a complete operating system]
  • Program virtual machine [designed for the execution of a single computer program]

Either type is limited to the resources provided by the virtual machine !insert image description here

Common Programming Language Types

There is an inseparable relationship between virtual machines and languages, so the Java virtual machine is also based on a statically compiled language similar to a virtual machine**.

Common programming languages ​​fall into three categories :

  1. Machine Language
    Explanation: Execute the binary instruction 0101 directly to the host without translation, and each operation code has a corresponding circuit inside the computer to complete it.
  2. assembly language
    Any low-level language used in electronic computers, microprocessors, microcontrollers, or other programmable devices. Most computers can be manipulated using mnemonic sequences rather than numeric manipulation codes.
  3. High-level language
    is convenient for everyone to understand, and then quickly design and implement program code. After the code is completed, it is converted into assembly or machine-corresponding execution code through compilation or interpretation, and then executed in the machine.
    insert image description here

Java cross-platform embodiment

The file forms compiled by different systems of C language are different, and
insert image description here
Java language is compiled into byte data through unified processing of virtual machine
insert image description here


    Java virtual machine composition

    The composition of the Java virtual machine is mainly composed of 4 parts

    • ClassLoader (class loader)
    • Runtime Data Area (runtime data area, memory partition)
    • Execution Engine
    • Native Interface (native library interface)

    core process

    insert image description here

    ClassLoader (class loader)

    ClassLoader is responsible for loading bytecode files, that is, class files. Class files have specific file marks at the beginning of the file, and ClassLoader is only responsible for loading class files.

    Class life cycle and loading process

    There are 7 stages in the life cycle of a class in the JVM.
    They are Loading, Verification, Preparation, Resolution, Initialization, Using, and Unloading . The first five parts (loading, verification, preparation, parsing, initialization) are collectively referred to as class loading.
    insert image description here
    Loading

    It is to find the "class file" in the file system / in the jar package / or wherever it exists. If the binary representation cannot be found, a NoClassDefFound error will be thrown.
    The load phase does not check the syntax and format of the classfile.

    Check (Loading)

    The first stage of the linking process is verification to ensure that the byte stream information in the class file meets the requirements of the current virtual machine and will not endanger the security of the virtual machine.

    Preparation

    Entering the preparation stage, this stage will create static fields, initialize them to standard default values ​​(such as null or 0 values), and allocate method tables, that is, allocate the memory space used by these variables in the method area.
    Resolution
    simply means that in the code we write, when a variable refers to an object, the reference is stored as a symbolic reference in the .class file (equivalent to making an index record). It needs to be resolved and linked as a direct reference (equivalent to pointing to the actual object) during the parsing phase. If there is a direct reference, the referenced target must exist in the heap. When loading a class, all super classes and super interfaces need to be loaded.
    Initialization (Resolution)

    The JVM specification clearly states that class initialization must be performed at the first "active use" of a class.
    The initialization process involves executing:

    • class constructor method static
    • static variable assignment statement
    • If the static static code block
      is initialized by a subclass, its parent class will be initialized first to ensure that its parent class is initialized before the subclass. So in fact, to initialize a class in java, the java.lang.Object class must be initialized first, because all java classes inherit from java.lang.Object.

    Execution Engine

    Execution engine, also called Interpreter. After the Class file is loaded, the instruction and data information will be put into the memory, and the Execution Engine is responsible for interpreting these commands to the operating system, that is, translating the JVM instruction set into the operating system instruction set

    Native Interface

    Responsible for invoking the local interface. The core function is to call the interfaces of different languages ​​for JAVA. It will record the corresponding local method in the Native Method Stack, and then load the corresponding local lib through the Execution Engine when calling the method.

    Runtime Data Area (runtime data area, memory partition)

    insert image description here

    Runtime Description Excerpt References "In-depth Understanding of JVM Virtual Machine-Third Edition" -Zhou Zhiming-Respect originality! !
    Program Counter
    Program Counter (Program Counter Register) is a small memory space, which can be seen as the line number indicator of the bytecode executed by the current thread. In the conceptual model of the Java virtual machine [1], the bytecode interpreter selects the next bytecode instruction to be executed by changing the value of the counter when it works. It is an indicator of the program control flow, branch, loop , Jump, exception handling, thread recovery and other basic functions need to rely on this counter to complete.

    Since the multithreading of the Java virtual machine is implemented by switching threads in turn and allocating processor execution time,
    a processor (a core for a multi-core processor) will only execute instructions in one thread. Therefore, in order to return to the correct execution position after thread switching, each thread needs to have an independent program counter. The counters between threads do not affect each other and are stored independently. We call this type of memory area "thread private". of memory.

    The Java virtual machine stack
    is the same as the program counter, **Java Virtual Machine Stack (Java Virtual Machine Stack)** is also thread-private, and its life cycle is the same as the thread. The virtual machine stack describes the thread memory model of Java method execution: when each method is executed, the Java virtual machine will synchronously create a stack frame [1] (Stack Frame) to store local variable tables, operand stacks, dynamic Information about connections, method exits, etc. The process of each method being called until the execution is completed corresponds to the process of a stack frame from being pushed to being popped in the virtual machine stack.
    Because the multithreading of the Java virtual machine is realized by switching threads in turn and allocating processor execution time.

    native method stack

    Native Method Stacks (Native Method Stacks) are very similar to virtual machine stacks. The difference is that the virtual machine stack serves the virtual machine to execute Java methods (that is, bytecodes), while the native method stack serves the virtual machine. The local (Native) method service used by the machine.

    Java heap
    For Java applications, the Java heap (Java Heap) is the largest piece of memory managed by the virtual machine. The Java heap is a memory area shared by all threads and created when the virtual machine starts. The sole purpose of this memory area is to store object instances, and "almost" all object instances in the Java world allocate memory here.

    insert image description here

    Method Area
    Method Area (Method Area), like the Java heap, is a memory area shared by each thread. It is used to store data such as type information, constants, static variables, and code caches compiled by the real-time compiler that have been loaded by the virtual machine .

    **Runtime Constant Pool** is part of the method area. In addition to the class version, fields, methods, interfaces and other description information in the Class file, there is also a constant pool table (Constant Pool Table), which is used to store various literals and symbol references generated during compilation. This part The content will be stored in the runtime constant pool in the method area after the class is loaded.


      Reference
      Zhou Zhiming - In-depth understanding of JVM data

      Guess you like

      Origin blog.csdn.net/jjc120074203/article/details/128921376