• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

What is JDK, JRE and JVM

King Wang

1 月 3, 2022

List of articles

    • What is Java JDK,JRE and JVM —In-depth Analysis( In depth analysis )
      • 1. Java Execution of procedures
      • 2. What is? JVM
        • 2.1 JVM structure
          • 2.1.1 Class Loader
          • 2.1.2 JVM Memory Areas
        • 2.2 JVM Execution Engine( Execution engine )
        • 2.2.1 Interpreter( Interpreter )
        • 2.2.2 JIT Compiler( Just in time compiler )
      • 3. What is? JRE
      • 4. What is? JDK
      • 5. JDK,JRE,JVM Differences between
      • 6. JDK,JRE,JVM Relevant interview questions

What is Java JDK,JRE and JVM —In-depth Analysis( In depth analysis )

Study “JDK,JRE,JVM The difference between .JVM How the interior works ? What is? class loaders( Class loader ),interpreter( Interpreter ),JIT compliers(JIT` Just in time compiler )

1. Java Execution of procedures

In the deep Java Before the inside , Let’s understand first Java How the source file is executed .

  1. Use an editor or IDE( Integrated development environment ) To write Java Source file . namely Simple.java
  2. The program must be compiled as a bytecode file ,javac(Java compiler ) Compile the source file as Simple.class file .
  3. Class files are available on any platform / The operating system consists of JVM(Java virtual machine ) perform
  4. JVM Translate bytecode files into machine code that the machine can execute (0,1 Binary system )

Java Execution Flow

2. What is? JVM

JVM(JAVA virtual machine ) Is to run Java The virtual machine of bytecode , Compile .java File for .class File gets bytecode file . .class File contains JVM Understandable bytecode .

In the real world ,JVM It’s a norm , It provides executable Java Bytecode runtime environment . Different vendors offer different implementations of this specification , Accessible Different JVM implementations see

The most popular JVM The implementation of Hotspot, It is from Oracle The company owns and maintains .

JVM Using many advanced technologies for Java Programs provide the best performance , Include advanced Memory model ,GC( Garbage collector ) and adaptive optimizer( Adaptive optimizer )

JVM There are two different styles –client and server. although Server and Client VMs be similar , however Server VM After special tuning , It can maximize the peak operating speed . It is used to execute long-running server applications , These applications need to run as fast as possible , Instead of fast startup time or smaller runtime memory footprint . Developers can specify -client or -server To choose the system they want .

JVM It’s called a virtual machine , Because it provides a machine interface independent of the underlying operating system and machine hardware architecture .

This independence from hardware and operating system makes Java Program “ Write once , Run anywhere ”(write-once-run-anywhere).

2.1 JVM structure

JVM Architecture

2.1.1 Class Loader

Class loader is a subsystem used to load class files , It has three main functions :loading( load ),linking( link ),initialization( initialization ).

  1. Loading
    • Load class ,JVM There are three ways to load :Bootstrap,extension,application
    • When loading a class file ,JVM It will find some arbitrary class XYZ.class The dependencies of .
    • The first bootstrap class loader tries to find the class , It will scan lib Under folder rt.jar file
    • If the class is not found , be extension The class loader will be in jre\lib\ext Find the class under the folder
    • Again, no classes were found ,application Class loaders will be in the system CLASSPATH Query all of the Jar Documents and classes .
    • If a class is found by any loader , Is loaded by the class loader , Otherwise, throw an exception :ClassNotFoundException.
  2. Linking
    • After class loading , perform Linking( link ), A bytecode verifier will verify that the generated bytecode is correct , If validation fails , Get a validation error . here , Memory is also allocated to static variables and static methods in the class .
  3. Initialization
    • This is class loaded The last stage , All static variables are assigned the original initial value , And execute the static code block .
2.1.2 JVM Memory Areas

JVM Memory areas are divided into parts to store specific parts of application data .

  • **Method Area( Method area ): ** Storing pixel data , Constant run pool , Structure of method code .

  • Heap( Heap area ): Stores all objects created by the program at runtime

  • Stack( The stack area ): Store local variables ( local variable ) And intermediate results . All of these variables are local to the thread that created them . Each thread has its own Java Stack, This area is created when the thread is created , All of these local variables are also known as : Thread local variable

  • PC register( Program counter ): Stores the physical memory address of the statement that is currently executing . stay Java in , Each thread has its own independent PC register.

  • Java Support and use native code . A lot of the underlying code is made up of C/C++ To write . The local method stack holds the instructions of the local code .

2.2 JVM Execution Engine( Execution engine )

All assigned to JVM All of the code is made up of Execution Engine( Execution engine ) perform , The execution engine reads the bytecode and executes one by one . It uses two built-in interpreters and JIT The compiler converts bytecode into machine code and executes it .

Platform Specific Interpreters

Use JVM, Both interpreters and compilers generate native code , The difference is how they generate native code , How to optimize and the cost of optimization .

2.2.1 Interpreter( Interpreter )

By looking for predefined JVM- Instructions Mapping to machine instructions ,JVM The interpreter can convert each bytecode instruction into the corresponding local instruction . It directly executes bytecode , No optimizations .

2.2.2 JIT Compiler( Just in time compiler )

  • In order to improve efficiency ,JIT Compiler At runtime with JVM Interaction ,, And properly compile the bytecode sequence into local machine code . Typically ,JIT Compiler Execute a piece of code ( Not one sentence at a time ), Optimize this code , And translate it into optimized machine code .
  • JIT Compiler Is turned on by default , You can disable , In this case , Will explain the whole thing Java Program , Therefore, it is not recommended to ban JIT Compiler, Except, of course, diagnosis or appearance JIT Compilation problems .

3. What is? JRE

Java Runtime environment (JRE) It’s a software package , It’s tied libraries(jar) and JVM, And with Java Other components of the written application .JVM It’s just JRE Part of the distribution .

Carry out any Java Program , It needs to be installed on the machine JRE, This is the minimum requirement .

JRE The bundled components are as follows :

  • DDL file : from Java The client virtual machine uses .
  • The code base , Property settings , Resource files also contain , such as rt.jar and charsets.jar
  • Java Extension files , Such as localedata.jar
  • Contains files for security management , These files include security policies (security policy) And security properties (security properties)
  • contain applets Support class’s jar file
  • Including the platform used TrueType The font file

JRE It can be used as JDK You can download part of it or you can download it separately ,JRE Depends on the platform , This means that depending on the type of machine ( Operating system and Architecture ), You must select… To import and install JRE package

for example : You can’t install 64 Bit JRE To 32 On the machine , Again , in the light of windows The distribution of cannot be in Linux Working on the platform .

4. What is? JDK

JDK yes JRE Superset ,JDK Contains JRE All development of , Tools for debugging and monitoring applications . When it comes to developing Java Application time , Need to install JDK.

Here is JDK Some of the important components that come with it :

  • apt Annotation processing tools
  • javadoc Document generator , Can automatically generate documentation from source code
  • jar Filing device , Package the relevant class libraries into a JAR In file . It can also help manage JAR file
  • jConsole Java Monitoring and management platform
  • jhat Java Heap analysis tools
  • jstack Print Java Thread stack information
  • keytool Policy creation and management tools
  • jarsigner Java Signature and verification tools

Same as JRE,JDK It also depends on the platform , So download the corresponding machine JDK package

5. JDK,JRE,JVM Differences between

Based on the above description , We can draw such a difference :

JRE = JVM + libraries to run Java Application
JDK = JRE + tools to develop Java Application

JDK vs JRE vs JVM

Simply speaking , If you write code Java Application developers , You will need to send the installation JDK, If you just want to run, use Java Built applications , You just need to install JRE that will do .

6. JDK,JRE,JVM Relevant interview questions

  • Java There are several types of class loading in

    Three , Namely :Bootstrap,extendsion,application

  • How class loaders work

    Class loaders scan their predefined jar Location of files and classes . They scan all the class files in the path and look for the required classes . If a class is found, load , Link and initialize class files .

  • JRE and JVM Different

    JVM Is the specification of the runtime environment , It performs Java Applications .Hotspot JVM It’s an implementation of this specification . It loads class files , Use interpreter and JIT Compiler Convert bytecode to machine code and execute it .

  • interpreter and JIT compiler Different

    interpreter Interpret bytecode line by line and execute in sequence . This can lead to poor performance .JIT compiler By analyzing the code in blocks and adding optimizations to the process , Then prepare ( compile ) For more optimized machine code .

发表回复