How JVM works, how jvm works

Source: Internet
Author: User

How JVM works, how jvm works

I. JVM Lifecycle

The life cycle of a Java VM is a clear task for executing Java programs. It runs only when the program starts to run, and stops when the program ends. When you run three programs on the same machine, there will be three running Java virtual machines. The Java Virtual Machine always starts with a main () method. This method must be public, return void, and directly accept a string array. During program execution, you must specify the class name for the main () method for the Java Virtual Machine. The Main () method is the starting point of the program. The executed thread is initialized as the initial thread of the program. Other threads in the program are started by him. There are two types of threads in Java: daemon and non-daemon ). The daemon thread is a thread used by the Java Virtual Machine. For example, the thread responsible for garbage collection is a daemon thread. Of course, you can also set your program as a daemon thread. The initial thread containing the Main () method is not a daemon thread. As long as there are common threads in the Java Virtual Machine for execution, the Java Virtual Machine will not stop. If you have sufficient permissions, you can call the exit () method to terminate the program.

Ii. Java code compilation and execution process

Developers write Java code (. java file), and then compile it into a bytecode file (. class file), and then the bytecode is loaded into the memory. Once the bytecode enters the virtual machine, it will be interpreted and executed by the interpreter, or converted to machine code by the real-time code generator.

(1) Java code compilation is completed by the Java source code compiler, that is, the process from Java code to JVM bytecode.

Source code, lexical analyzer, syntax analyzer, semantic analyzer, bytecode generator, JVM bytecode

(2) Java bytecode execution is completed by the JVM execution engine.

JVM bytecode and bytecode Interpreter

JVM bytecode, machine-independent optimization, machine-related optimization, register distributor, target code generator, and target code

 

The entire Java code compilation and execution process includes three important mechanisms: java source code compilation, class loading, and class execution.

 

1. Java source code compilation mechanism (analysis and output to symbol tables, annotation processing, semantic analysis, and generation of class files)

The generated class file consists of the following parts:

Structure Information: including the version number of the class file format and the quantity and size of each part

Metadata: corresponds to the declaration and constant information in Java source code. Contains the declaration information, domain and method declaration information, and constant pool of classes/inherited superclasses/Implemented interfaces

Method information: information corresponding to statements and expressions in Java source code. Including bytecode, exception processor table, size of evaluation stack and local variable area, type record of evaluation stack, debugging Symbol Information

2. Class Loading Mechanism

Each Java virtual machine has a class loader subsystem, which is responsible for loading types (classes and interfaces) in the program and giving a unique name. Each Java virtual machine has an execution engine that executes the commands contained in the loaded class.

JVM class loading is completed through ClassLoader and its subclass.

Bootstrap ClassLoader: responsible for loading all classes in jre/lib/rt. jar in $ JAVA_HOME, implemented by C ++, not a ClassLoader subclass.

Extension ClassLoader: responsible for loading some jar packages of the java platform's Extension functions, including jre/lib/* in $ JAVA_HOME /*. jar or-Djava. ext. jar package in the directory specified by dirs

App ClassLoader: loads the jar package of the specified classpath in the startup parameter and the class in the directory.

User-Defined loader (Custom ClassLoader): belongs to the ClassLoader defined by the application according to its own needs. For example, tomcat and jboss will implement their own ClassLoader according to j2ee specifications.

During the loading process, the system first checks whether the class has been loaded. The check sequence is from bottom to top. From Custom ClassLoader to BootStrap ClassLoader, this class is regarded as loaded as long as a classloader has been loaded, make sure that this class is only loaded once by all ClassLoader. The loading order is from top to bottom, that is, the upper layer tries to load this class layer by layer.

Load:

1. Get the binary byte stream defining this class through the "full name of the class"

2. Convert the static storage structure represented by byte streams into the runtime data structure of the method zone.

3. Generate a java. lang. Class Object that represents the Class in the java heap and serve as the access entry to the data in the method area.

 

Iii. Class execution mechanism

JVM is a stack-based virtual machine. JVM allocates a stack for each newly created thread. That is to say, for a Java program, its operation is done through stack operations. The stack stores the thread status in frames. JVM only performs two types of operations on the stack: frame-based stack pressure and outbound stack operations.

Stack frames are composed of three parts: local variable zone, operand stack, and frame data zone.

The JVM executes the class bytecode. After a thread is created, the program counter (PC) and Stack are generated. The program counter stores the offset of the next instruction to be executed in the method, stack stores stack frames. Each stack frame corresponds to each call of each method, and the stack frame consists of a local variable zone and an operand stack, the local variable area is used to store local variables and parameters in the method. the operand stack is used to store intermediate results generated during method execution.

Ii. java garbage collection mechanism

Typical garbage collection algorithms

(1) Mark-Sweep Algorithm: This is the most basic garbage collection algorithm. The most fundamental reason is that it is the easiest to implement and the simplest to think about. The tag-clearing algorithm is divided into two phases: the tag phase and the clear phase. The task in the mark phase is to mark all objects to be recycled, and the clear phase is to recycle the space occupied by the marked objects. The specific process is shown in:

 

 

It is easy to see that the tag-clearing algorithm is easier to implement, but a serious problem is that memory fragments are easily generated, too many fragments may cause insufficient space to be found when large objects need to be allocated in the subsequent process, and a new garbage collection action will be triggered in advance.

(2) Copying algorithm: To solve the defects of the Mark-Sweep algorithm, the Copying algorithm is proposed. It divides the available memory into two equal-size blocks by capacity and uses only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space, in this way, memory fragmentation is not prone. The specific process is shown in:

 

 

Although this algorithm is easy to implement and runs efficiently, it is not easy to generate memory fragments, but it imposes a high cost on the use of memory space, because the memory that can be used is halved. Obviously, the efficiency of the Copying algorithm is significantly related to the number of surviving objects. If there are many surviving objects, the efficiency of the Copying algorithm will be greatly reduced.

(3) Mark-Compact (Mark-arrangement) algorithm: To solve the Copying algorithm's defects and make full use of the memory space, a Mark-Compact algorithm is proposed. The algorithm Mark phase is the same as Mark-Sweep, but after the Mark is completed, it does not directly clear the recyclable object, but moves the surviving object to one end, then, the memory outside the end boundary is cleared. The specific process is shown in:

 

 

(4) Generational Collection (Generational Collection) algorithm:

The generational collection algorithm is currently used by most JVM garbage collectors. Its core idea is to divide the memory into several different regions based on the lifecycle of the object. Generally, heap zoning is divided into Tenured Generation and Young Generation. In the old age, only a few objects need to be recycled during each garbage collection, the new generation is characterized by the fact that a large number of objects need to be recycled during each garbage collection, the most suitable collection algorithm can be adopted based on the characteristics of different generations.

At present, most garbage collectors adopt the Copying Algorithm for the new generation, because most objects need to be recycled in each garbage collection in the new generation, that is, the number of replication operations is small, however, in reality, the new generation space is not divided by a ratio of. Generally, the new generation space is divided into a larger Eden space and two smaller than vor spaces, each time you use the Eden space and one of the other vor spaces, When you recycle the space, copy the surviving objects in the Eden and other vor to the other vor space, then, clear the Eden and the used volume vor space.

In the old age, only a small number of objects are recycled each time. Generally, the Mark-Compact algorithm is used.

Note that there is also a permanent Generation (Permanet Generation) outside the heap, which is used to store class classes, constants, and method descriptions. The recovery of permanent generation mainly involves two parts: discard constants and useless classes.

 

Two important methods:

System. gc () method: using System. gc () can request Java garbage collection regardless of the garbage collection algorithm used by JVM.

Finalize () method: Before the JVM Garbage Collector collects an object, it is generally required that the program call an appropriate method to release the resource, but without explicitly releasing the resource, java provides a default mechanism to terminate this object and release resources. This method is finalize (). Its prototype is protect void finalize () throws Throws

Throwable after the finalize () method returns, the object disappears and the garbage collection starts to be executed. Throws in the prototype

Throwable indicates that it can throw any type of exception.

The reason for using finalize () is that the Garbage Collector cannot process it. For example, open file resources are not within the recycle range of the garbage collector.

In addition:

(1) Each object can only call the finalize () method once. If an exception occurs during execution of the finalize () method, the object can still be collected by the garbage collector.

The state of the object in the JVM heap area:

(1) touchable status: if there is a variable reference in the program, this status is touchable.

(2) recoverable state: when no variable in the program references this object, the object is changed from touchable state to recoverable state. The GC thread will prepare to call the finalize () method of this object at a certain time. The code in the finalize method may convert the object to the touchable state, otherwise the object will be converted to the intouchable state.

(3) Inaccessibility: GC threads can reclaim the memory of an object only when the object is inaccessibility.

 

In addition: http://www.open-open.com/lib/view/open1408453806147.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.