What is the JVM

A: A virtual machine is an abstract computer that simulates various computer functions on an actual computer. Java virtual machines have their own complete hardware architecture, such as processors, stacks, registers, etc., as well as corresponding instruction systems. The Java Virtual machine masks information specific to a particular operating system platform, allowing Java programs to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java Virtual machine. Simply put, the JVM is used to parse and run Java programs.

A very important feature of the Java language is its platform-independence. The use of Java virtual machine is the key to achieve this feature. Normal high-level languages need to be compiled into at least different object code if they are to run on different platforms. With the introduction of the Java language VIRTUAL machine, the Java language does not need to be recompiled when running on different platforms. The Java language uses the Java Virtual machine to mask platform-specific information, allowing Java language compilers to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java Virtual machine. When the Java virtual machine executes bytecodes, it interprets the bytecodes as machine instructions on a specific platform. This is why Java can compile once and run anywhere

The location of the JVM

The overall structure of the JVM

1) The Class loading subsystem is responsible for loading Class information from the file system or network. The loaded Class information is stored in a memory space called the method area. In addition to Class information, the method area may also hold runtime constant pool information, including string literals and numeric constants (this constant information is a memory map of the constant pool portion of the Class file).

2) The Java heap is created during virtual machine startup and is the primary memory working area for Java programs. Almost all Java object instances are stored in the Java heap. The heap space is shared by all threads and is a piece of memory that is closely related to Java applications.

3) Java’s NIO library allows Java programs to use direct memory. Direct memory is the memory space outside the Java heap that is requested directly from the system. Direct memory is usually faster to access than the Java heap. For performance reasons, direct memory may be used in situations where read and write are frequent. Since the direct internals reside outside the Java heap, its size is not directly limited by the maximum heap size specified by Xmx, but system memory is finite, and the sum of the Java heap and direct memory is still limited by the maximum memory the operating system can give.

4) The garbage collection system is an important part of the Java virtual machine. The garbage collector can recycle the method area, Java heap and direct memory. The Java heap is the focus of the garbage collector. Unlike C/C++, all object-space frees in Java are implicit, that is, there are no Java functions such as free() or delete() to free a specified memory region. For garbage objects that are no longer in use, the garbage collection system works silently in the background to find, identify, and release garbage objects, achieving full automated management in the Java heap, method area, and direct memory.

5) Each Java virtual machine thread has a private Java stack. The Java stack of a thread is created when the thread is created. The Java stack holds frame information, local variables and method parameters, and is closely related to the invocation and return of Java methods.

6) The native method stack is very similar to the Java stack, the biggest difference being that the Java stack is used for method calls, while the native method stack is used for local method calls. As an important extension to the Java Virtual Machine, the Java Virtual Machine allows Java to call native methods directly (usually written in C).

7) The PC (Program Counter) register is also a private space for each thread, and the Java Virtual machine creates a PC register for each Java thread. At any given moment, a Java thread is always executing a method, and the method being executed is called the current method. If the current method is not a local method, the PC register points to the instruction currently being executed. If the current method is a local method, the value of the PC register is undefined

8) Execution engine is one of the most core components of Java virtual machine, which is responsible for the execution of virtual machine bytecode. Modern virtual machines use just-in-time compilation technology to compile methods into machine code before execution in order to improve execution efficiency.

Java code executes the process

Java programs — (compile) –> bytecode files — (explain execution) –> Operating system (Win, Linux, Mac JVM)

Instruction set architecture for stacks and instruction set architecture for registers

Due to the cross-platform design, Java instructions are designed according to the stack, different platforms CPU architecture is different, so it can not be designed as register-based stack: cross-platform, small instruction set, many instructions; Less execution than register Register: Fewer instructions

Javap -v StackStruTest. Class // Prints the process JPS executed by the programCopy the code

JVM life cycle