In-depth understanding of JVM (1) - basic principles

foreword

  The JVM has always been an important part of the advanced stage of java knowledge. If you want to study more deeply in the field of java, the JVM is a topic that cannot be avoided no matter what. This series attempts to explain the JVM necessary in a concise and easy-to-read way. knowledge points.

run process

  We all know that the slogan that java has been promoting is: compile once, run everywhere. So how does it work? Let's look at the picture below:


 After the java program is compiled once, the java code is compiled into bytecode, that is, a class file, and then interpreted by different java virtual machines on different operating systems, and finally converted into machine code of different platforms, and finally executed. In this way, we can deduce whether we only need to install the mac java virtual machine if we want to run it on the mac system. So after understanding this basic principle, we try to do a deeper research, what is the execution process of an ordinary java program? For example, we wrote a piece of code like this:

public class HelloWorld { public static void main(String[] args) { System.out.print("Hello world"); } }

What steps did this program go through from compiling to running and finally printing "Hello world"? We go directly to the picture:

After the java code is compiled, a bytecode file (class file) is generated and executed through: java HelloWorld. At this time, java finds jvm.cfg according to the system version. You can search where the jvm.cfg file on your computer is, it will be based on the system version. Your system version is placed in a different location. For example, my file is in: C:\Program Files\Java\jdk1.8.0_101\jre\lib\amd64\jvm.cfg, open it and take a look:


This is the file on my computer, where -server KNOWN means that the jvm named server is available. If you search for jvm.dll on your computer at this time, you will find that it must be in one of your server directories, such as mine: C:\Program Files\Java\jdk1.8.0_101\jre\bin\server \jvm.dll. In short, the corresponding jvm.dll is found through the jvm.cfg file, and jvm.dll is the main implementation of the java virtual machine. Next, the JVM will be initialized, and the JNI interface will be obtained. What is the JNI interface, which is the java native interface, do you think java is compiled into a class file, how does the JVM find this file from the hard disk and load it into the JVM, that is, through JNI Interface (it is also often used for java to interact with the operating system and hardware), find the class file and load it into the JVM, then find the main method, and finally execute it.

JVM basic structure

  Maybe through the above description, you have a rough understanding of the JVM running process, so how does the JVM execute a class file internally, that is, what are the internal details of the last step and the sixth step in the above figure? To understand this, we first have to look at the internals of the JVM:


It is not difficult to see from this structure that after the class file is loaded by the jvm, after the memory space allocation of the jvm, the execution engine finally completes the execution of the class file. Of course, there are other role modules in this process. These modules work together to make a java program run successfully. These templates are described in detail below. They are also the most important part of learning jvm later.

Memory space:

The JVM memory space includes: method area, java heap, java stack, and local method stack.

The method area is an area shared by each thread, which stores class information, constants, and static variables.

The java heap is also an area shared by threads. The instance of our class is placed in this area. It is conceivable that one of your systems will generate many instances, so the space of the java heap is also the largest. If the java heap space is insufficient, the program will throw an OutOfMemoryError exception.

java栈是每个线程私有的区域,它的生命周期与线程相同,一个线程对应一个java栈,每执行一个方法就会往栈中压入一个元素,这个元素叫“栈帧”,而栈帧中包括了方法中的局部变量、用于存放中间状态值的操作栈,这里面有很多细节,我们以后再讲。如果java栈空间不足了,程序会抛出StackOverflowError异常,想一想什么情况下会容易产生这个错误,对,递归,递归如果深度很深,就会执行大量的方法,方法越多java栈的占用空间越大。

本地方法栈角色和java栈类似,只不过它是用来表示执行本地方法的,本地方法栈存放的方法调用本地方法接口,最终调用本地方法库,实现与操作系统、硬件交互的目的。

PC寄存器,说到这里我们的类已经加载了,实例对象、方法、静态变量都去了自己改去的地方,那么问题来了,程序该怎么执行,哪个方法先执行,哪个方法后执行,这些指令执行的顺序就是PC寄存器在管,它的作用就是控制程序指令的执行顺序。

执行引擎当然就是根据PC寄存器调配的指令顺序,依次执行程序指令。

结语

  本文主要介绍了java虚拟机运行的基本流程,以及java虚拟机内部结构。下一篇我们将学习java内存模型以及探索java变量的可见性、有序性、指令重排等问题。




Guess you like

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