Detailed explanation of the basic structure of JVM

What is Java Virtual Machine

There are two concepts involved here:

  • Java virtual machine , equivalent to the Java class we define
  • Java virtual machine instance , equivalent to new a Java class

Note: The Java virtual machine starts a Java virtual machine instance through java.exe or javaw.exe

JVM life cycle

There are two types of threads in the Java virtual machine:

  • Daemon threads, such as GC threads
  • Non-daemon threads, such as main function, custom thread

Note: In the java virtual machine, as long as any non-daemon threads have not ended, the instance of the java virtual machine will not exit.

The life cycle of the java virtual machine : When a java application main function starts, the virtual machine is also started at the same time, and only when all the non-daemon processes in the virtual machine instance end, the java virtual machine instance ends its life.

JVM architecture

Enter image description

Enter image description

  1. Class Loader (ClassLoader)

When a ClassLoader starts, the ClassLoader lives in the JVM heap, and then it loads the .class file from the file system or the network into the JVM's method area (permanent area) , and the byte file in the method area will be virtualized The machine takes a new object instance, and then generates a bytecode object in the JVM heap, and there are two references in the class bytecode memory file: one points to the class object instance in the heap, and the other points to the ClassLoader that loads itself.

2. Execution engine

The execution engine is one of the core components of the java virtual machine. It is responsible for executing the bytecode of the virtual machine. In order to improve the execution efficiency, modern virtual machines will use just-in-time compilation technology to compile methods into machine code and then execute them.

  1. Method area (permanent area)

The method area stores the class information loaded by the class loader, including class information (modifiers, class names, etc.), field attributes (modifiers, types, field names, etc.), method information (modifiers, return parameters, method names, Input parameters), class variables (that is, static fields modified by static), and so on. In addition to class information, the method area also stores constant pool information at runtime, including string literals and numeric constants

  1. Java heap

The java heap is created when the virtual machine starts and is the main memory working area of ​​the java program. Almost all java object instances are stored in the java heap. Heap space is shared by all threads.

  1. Java virtual machine stack

Each java thread has a private java virtual machine stack, which is created when the thread is created. The stack frame information is stored in the java stack, and a method corresponds to a stack frame (including method local variables, method parameters, etc.)

  1. native method stack

The native method stack is very similar to the java stack. The biggest difference is that the java stack is used for calling java methods, while the native method stack is used for calling native methods.

Summarize

When a program starts, its class file will be loaded into the method area (permanent area) by the class loader (ClassLoader) , the execution engine reads the bytecode of the method area for parsing, runs while parsing, and then the PC register points to the Where the main function is located, the virtual machine starts to reserve a stack frame in the Java virtual machine stack for the main function, and then starts to execute the main function. The code in the main function is mapped by the execution engine to the corresponding implementation in the local operating system, and then calls the local Method interface , when the native method is running, the operating system will allocate the native method stack for the native method to store some temporary variables, then run the native method, call the OS API, and so on.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324927877&siteId=291194637