当前位置:网站首页>JVM (1): introduction, structure, operation and lifecycle

JVM (1): introduction, structure, operation and lifecycle

2022-06-11 04:16:00 Prince you

One 、 virtual machine

        The so-called virtual machine (Virtual Machine), It's a virtual computer , It's a software , Used to execute a series of virtual computer instructions . In general, virtual machine can be divided into system virtual machine and program virtual machine .

  • big-name Visual Box,VMWare It belongs to the system virtual machine , They are all simulations of physical computers , It provides a software platform that can run a complete operating system .
  • A typical representative of program virtual machine is java virtual machine , It's designed to execute a single computer program , stay java The instructions executed in the virtual machine are called java Bytecode instruction .

        Whether it's a system virtual machine or a program virtual machine , The software running on it is limited to the resources provided by the virtual machine .

Two 、java virtual machine

        java A virtual machine is an execution machine java Bytecode virtual computer , It has an independent operating mechanism , It runs java Bytecode may not be java Language compilation .

        JVM Various languages of the platform can be shared java The cross platform nature of virtual machine , Excellent garbage collection mechanism and reliable just in time compiler .java The core of technology is java virtual machine (JVM,Java Virtual Machine), Because of all the java The program is running in java Inside the virtual machine .

effect

        java Virtual machine is the running environment of binary bytecode , Responsible for loading bytecode into its interior , explain / Compile to execute machine instructions on the corresponding platform . Every one of them java Instructions ,java There are detailed definitions in the virtual machine specification , How to get the operands , How to deal with operands , Where is the result of the treatment .

characteristic

  • A compilation , Run anywhere .
  • Automatic memory management .
  • Automatic garbage collection function .

3、 ... and 、JVM The location of

        JDK The composition of the

  Four 、JVM The overall structure

  • Hotspot VM It is one of the representative works of high performance virtual machine on the market .
  • It uses an interpreter and just in time compiler architecture .
  • In today's ,Java The performance of the program has been completely changed , Have reached the point where you can and C/C++ The program is higher and lower .
     

          The method area and heap are shared by multiple threads , and java Stack 、 Local method stacks and program counters are unique to each thread .

Execution engine

          In the red box is the execution engine , From left to right : Interpreter 、JIT Just in time compiler 、 Garbage collector .

        JIT The just in time compiler is the back end of the compiler (java Source code compiled as class Files can also become compiler front ends ), Because the bytecode file cannot call the local machine program , The bytecode needs to be compiled into the local machine language .

5、 ... and 、java Code execution process

          The following figure is a detailed drawing :

  • Explain to perform : Guaranteed response time , Execute line by line response to bytecode file .
  • Compile implementation : The byte code with high repeatability ( Hot code ) Compile into machine instructions ( Recompilation ) And cache it .

6、 ... and 、JVM Architectural model

        Java The instruction stream base of compiler input is a stack based instruction set architecture , Another instruction set architecture is register based instruction set architecture . say concretely , The difference between the two architectures :

Based on the characteristics of plank structure

  • Simpler design and Implementation , For systems with limited resources .
  • Avoid the problem of register allocation : Use zero address instruction to assign .
  • Most of the instructions in the instruction stream are zero address instructions , Its execution depends on the operation stack . The instruction set is smaller , The compiler is easy to implement .
  • No hardware support required , Better portability , Better cross platform .

Based on the characteristics of register architecture

  • The typical application is x86 The binary instruction set of ; Like traditional Pc as well as Android Of Davlik virtual machine .
  • The instruction set architecture is completely hardware dependent , Poor portability
  • Better performance and more efficient execution .
  • Spend less instruction to complete an operation .
  • In most cases , The instruction set based on register architecture usually uses one address instruction 、 Two address instructions and three address instructions are the main , The instruction set based on the stack architecture is mainly zero address instruction .

         summary :

         Because of the cross platform design ,Java The instructions of are designed according to the stack . Different platforms CPU Different architectures , So it can't be designed as register based . The advantage is cross platform , Instruction set is small , The compiler is easy to implement , The disadvantage is that the performance is reduced , More instructions are needed to implement the same function .

         today , Although embedded platforms are no longer Java The mainstream running platform of the program ( To be exact, it should be HotSpotVM Our host environment is no longer limited to embedded platforms ), So why not change the architecture to register based architecture ?

7、 ... and 、JVM Life cycle

Start of virtual machine

        Java The virtual machine is started by booting the classloader (bootstrap class loader) Create an initial class (initial class) To complete , This class is specified by the specific implementation of the virtual machine .

Virtual machine execution

  • A running Java The virtual machine has a clear task : perform Java Program .·
  • It only runs when the program starts executing , It stops at the end of the program .
  • Carry out a so-called Java When it comes to programming , What is really being implemented is a project called Java Process of virtual machine .

Virtual machine exit

         There are several situations as follows :

  • The normal execution of the program ends
  • The program encountered an exception or error during execution and terminated abnormally
  • Caused by an error in the operating system Java Virtual machine process termination
  • A thread calls Runtime Class or system Class exit Method , or Runtime Class halt Method , also Java Security manager also allows this time exit or halt operation .
  • besides ,JNI ( Java Native Interface) The specification describes the use of JNI Invocation API To load or unload Java When virtual machines ,Java Virtual machine exit .
原网站

版权声明
本文为[Prince you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110405100330.html