Java JVM explain the working principle and process

Speaking of Java, people first think of the Java programming language, but in fact, Java is a technology, which consists of four areas composed of: Java programming language, Java class file format, Java Virtual Machine and Java application programming interface (Java API) . Their relationship is as shown below:

Runtime Environment on behalf of the Java platform, written in Java code (.java file) developer, and then compile it into bytecode (.class files), and then the byte code is loaded into memory, once the bytecode VM entry it is interpreted executed by an interpreter, or machine code is executed to-time code generator has selected conversion.

Java Platform Interface built by the Java Virtual Machine and Java applications, Java is the language into the channel of this platform, written in the Java programming language and compiled to run on this platform. The platform structure shown below:

In the structure of the Java platform, it can be seen, Java Virtual Machine (JVM) in a central location, is the key to the program and the underlying operating system and hardware independent. It was transplanted beneath the interface, the interface graft consists of two parts: the adapter and Java operating system, which relies on the platform known as an adapter portion; JVM implemented on a particular platform and operating system via a portable interface; is above the JVM Java the basic libraries and libraries as well as expand their API, using application (application) written in Java API and applet (Java applet) can run on any Java platform, regardless of the underlying platform, it is because the Java virtual machine ( JVM) to achieve the separation procedure and the operating system, enabling the Java platform independence. 

JVM has in its life cycle in a clear mandate, and that is to run Java programs, so when a Java program starts, it creates an instance of the JVM; when the end of the program run, the instance disappears with it. Let's take a more in-depth research from its architecture and its JVM process running these two aspects.

1, the architecture of the Java Virtual Machine

• Each JVM has two mechanisms:

Class loader subsystem ①: having a suitable load class or interface name

② execution engine: responsible for executing instructions contained in the loaded class or interface 

• Each JVM contains :

The method area, Java heap, Java stacks, native method stacks, and other implicit instruction counter register

 

 

For the JVM to learn, so in my opinion the most important of several parts:

The whole process of compiling and executing Java code

JVM memory management and garbage collection

The following sections of these are described separately:

2, the whole process of compiling and executing Java code

Also as I said before, the whole process of compiling and executing Java code probably is: write Java code (.java file) developer, and then compile it into bytecode (.class files), and then the byte code is loaded into memory, once the bytecode in the virtual machine, it will be interpreted interpreter, or by-time code generator selectively converted into machine code execution.

(. 1) compiled Java code is accomplished by the Java source code compiler, i.e. Java code to process JVM byte code (.class files). Flowchart is as follows:

(2) execution of Java bytecode is done by the JVM execution engine, the flowchart as shown below:

 

Java code compilation and execution of the entire process includes the following three important mechanisms:

· Java source code compilation mechanism

· Class loading mechanism

· Class implementation mechanisms

 

(1) Java source code compilation mechanism

Java source code compilation consists of the following three processes:

① input to the symbol table and Analysis

② Annotation Processing

③ generating class files and semantic analysis

Flowchart is as follows:

Last generated class file consists of the following components:

① structural information: information on the number and size of each part, including the version number of the class file format

② Metadata: information corresponding to the Java source code statements and constants. Containing the class / superclass inheritance / implementation of the declaration of the interface information, domain information and method declaration and constant pool

③ method of information: the corresponding Java source code statements and expressions corresponding information. Comprising bytecodes, an exception handler table, the size of the evaluation stack with local variables, the type of recording evaluation stack, debug symbol information

(2) Class Loading
JVM class loader is accomplished by ClassLoader and its subclasses, the class hierarchy and the loading sequence can be described by the following diagram:

①Bootstrap ClassLoader

$ JAVA_HOME responsible for loading in jre / lib / rt.jar in every class, implemented by C ++, not ClassLoader subclass

②Extension ClassLoader

负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包

③App ClassLoader

负责记载classpath中指定的jar包及目录中class

④Custom ClassLoader

属于应用程序根据自身需要自定义的ClassLoader,如tomcat、jboss都会根据j2ee规范自行实现ClassLoader

 

加载过程中会先检查类是否被已加载,检查顺序是自底向上,从Custom ClassLoader到BootStrap ClassLoader逐层检查,只要某个classloader已加载就视为已加载此类,保证此类只所有ClassLoader加载一次。而加载的顺序是自顶向下,也就是由上层来逐层尝试加载此类。

(3)类执行机制

JVM是基于堆栈的虚拟机。JVM为每个新创建的线程都分配一个堆栈.也就是说,对于一个Java程序来说,它的运行就是通过对堆栈的操作来完成的。堆栈以帧为单位保存线程的状态。JVM对堆栈只进行两种操作:以帧为单位的压栈和出栈操作。

JVM执行class字节码,线程创建后,都会产生程序计数器(PC)和栈(Stack),程序计数器存放下一条要执行的指令在方法内的偏移量,栈中存放一个个栈帧,每个栈帧对应着每个方法的每次调用,而栈帧又是有局部变量区和操作数栈两部分组成,局部变量区用于存放方法中的局部变量和参数,操作数栈中用于存放方法执行过程中产生的中间结果。栈的结构如下图所示:

3、JVM内存管理及垃圾回收机制

JVM内存结构分为:方法区(method),栈内存(stack),堆内存(heap),本地方法栈(java中的jni调用),结构图如下所示:

(1)堆内存(heap)

所有通过new创建的对象的内存都在堆中分配,其大小可以通过-Xmx和-Xms来控制。 
操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删除,并将该结点的空间分配给程序,另外,对于大多数系统,会在这块内存空间中的首地址处记录本次分配的大小,这样代码中的delete语句才能正确的释放本内存空间。但由于找到的堆结点的大小不一定正好等于申请的大小,系统会自动的将多余的那部分重新放入空闲链表中。这时由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便。另外,在WINDOWS下,最好的方式是用VirtualAlloc分配内存,它不是在堆,也不是在栈,而是直接在进程的地址空间中保留一块内存,虽然这种方法用起来最不方便,但是速度快,也是最灵活的。堆内存是向高地址扩展的数据结构,是不连续的内存区域。由于系统是用链表来存储的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大

(2)栈内存(stack)

在Windows下, 栈是向低地址扩展的数据结构,是一块连续的内存区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的,在WINDOWS下,栈的大小是固定的(是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将提示overflow。因此,能从栈获得的空间较小。只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。 由系统自动分配,速度较快。但程序员是无法控制的。

堆内存与栈内存需要说明:

基础数据类型直接在栈空间分配,方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收。引用数据类型,需要用new来创建,既在栈空间分配一个地址空间,又在堆空间分配对象的类变量 。方法的引用参数,在栈空间分配一个地址空间,并指向堆空间的对象区,当方法调用完成后从栈空间回收。局部变量new出来时,在栈空间和堆空间中分配空间,当局部变量生命周期结束后,栈空间立刻被回收,堆空间区域等待GC回收。方法调用时传入的literal参数,先在栈空间分配,在方法调用完成后从栈空间收回。字符串常量、static在DATA区域分配,this在堆空间分配。数组既在栈空间分配数组名称,又在堆空间分配数组实际的大小。

如:

(3)本地方法栈(java中的jni调用)

用于支持native方法的执行,存储了每个native方法调用的状态。对于本地方法接口,实现JVM并不要求一定要有它的支持,甚至可以完全没有。Sun公司实现Java本地接口(JNI)是出于可移植性的考虑,当然我们也可以设计出其它的本地接口来代替Sun公司的JNI。但是这些设计与实现是比较复杂的事情,需要确保垃圾回收器不会将那些正在被本地方法调用的对象释放掉。

(4)方法区(method)

它保存方法代码(编译后的java代码)和符号表。存放了要加载的类信息、静态变量、final类型的常量、属性和方法信息。JVM用持久代(Permanet Generation)来存放方法区,可通过-XX:PermSize和-XX:MaxPermSize来指定最小值和最大值。

垃圾回收机制

堆里聚集了所有由应用程序创建的对象,JVM也有对应的指令比如 new, newarray, anewarray和multianewarray,然并没有向 C++ 的 delete,free 等释放空间的指令,Java的所有释放都由 GC 来做,GC除了做回收内存之外,另外一个重要的工作就是内存的压缩,这个在其他的语言中也有类似的实现,相比 C++ 不仅好用,而且增加了安全性,当然她也有弊端,比如性能这个大问题。

 

4、Java虚拟机的运行过程示例

上面对虚拟机的各个部分进行了比较详细的说明,下面通过一个具体的例子来分析它的运行过程。

虚拟机通过调用某个指定类的方法main启动,传递给main一个字符串数组参数,使指定的类被装载,同时链接该类所使用的其它的类型,并且初始化它们。例如对于程序:

编译后在命令行模式下键入: java HelloApp run virtual machine 

将通过调用HelloApp的方法main来启动java虚拟机,传递给main一个包含三个字符串"run"、"virtual"、"machine"的数组。现在我们略述虚拟机在执行HelloApp时可能采取的步骤。

开始试图执行类HelloApp的main方法,发现该类并没有被装载,也就是说虚拟机当前不包含该类的二进制代表,于是虚拟机使用ClassLoader试图寻找这样的二进制代表。如果这个进程失败,则抛出一个异常。类被装载后同时在main方法被调用之前,必须对类HelloApp与其它类型进行链接然后初始化。链接包含三个阶段:检验,准备和解析。检验检查被装载的主类的符号和语义,准备则创建类或接口的静态域以及把这些域初始化为标准的默认值,解析负责检查主类对其它类或接口的符号引用,在这一步它是可选的。类的初始化是对类中声明的静态初始化函数和静态域的初始化构造方法的执行。一个类在初始化之前它的父类必须被初始化。整个过程如下:

发布了27 篇原创文章 · 获赞 8 · 访问量 1万+

Guess you like

Origin blog.csdn.net/gonghua0502/article/details/87857529