Java Virtual Machine (JVM--Java Virtual Machine)

preface

There are a lot of articles about the Java virtual machine, and this article is intended to sort them out and make it easier to flip through later. In addition, the diagram of the online article is quite ugly, 90% of the diagram of this article are from the next hand, there is an error in the diagram, please point out, must be corrected immediately this article mainly introduces the Java virtual machine abstract structure and some basic conceptsCopy the code

A brief introduction to several concepts

JDK, JRE, and JVM
With the JRE, you can run Java programs. If you just run software, install the JRE. We generally say that Java8, Java10 are referred to the JDK, Java developers use a set of tools, is a big concept, the following is java8 JDK componentsCopy the code


2. Relationship between JDK, JRE, and JVM

Java Runtime Environment (JRE) JVM: Java Virtual Machine (Java Virtual Machine)


3. Historical Java VM types

Virtual machines (VMS) simulate computer functions and provide unified operation interfaces to achieve code consistency across different platforms. At its core, the JVM is an abstract interface that has many implementations (see below), and those implementations are just applications. There have been iterations of JVMS throughout Java’s history, as well as JVMS for different scenarios.

The Sun Classic VM is the first commercial Java VIRTUAL machine that executes Java code in a pure interpreter fashion. EXact VM compiler interpreter mix work, It was soon replaced by HotSpot VM (out of use). HotSpot VM is still used today on KVM mobile ---- inefficient (out of use). JRockit focuses on server applications J9 IBM Microsoft JVM Windows ---- platform dedicated (has been removed from the stage of history) Taobao VM Taobao VM Dalvik Android VM based on HotSpot VM, register architecture, execute. Dex file (.class-->.dex)Copy the code

4.Java VM structure


2. Brief introduction to Java VM architecture

1..classFiles andClass loader
1-1. .classfile

If you are interested in class, please refer to the official documentation. The class file is introduced in detail, and I will write a special article in the future. Java learned from day one that the javac command could generate.class files from.java, and then used the IDE, and basically did not use class files


1-2. Classloader subsystem

The ClassLoader sub-system is used to load Java bytecode files into the JVM.


2. Run time data area
2-1. Program counter
The current thread is private, that is, each thread has one, which can be changed to select the next bytecode instruction that needs to be executed. Memory is small, and the only thing in the JVM that doesn't have OutOfMemoryError is if the thread is executing a local method, then the program counter is undefined.Copy the code

2-2. Java virtual machine stack
Adjusting parameters: - Xss = -- -- -- - > [1. Stack frame (StatckFrame)] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | -- - a thread of each method in the execution at the same time, will create a stack frame. | - stack frames stored in the local variables, operation station, dynamic links, export, etc. | - when the method is invoked, the stack frame into the stack when the method completes, the stack frame out of the stack. -- -- -- - > [2. Other related] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | - access speed, second only to register | - storage method of relevant local variables, including all kinds of basic data type, object of reference, such as the return address. | - local variables of the memory space distribution of the completion of the compiler, runtime is unlikely to change its size -- -- -- - > [3. Abnormal related] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- stack overflow: Memory overflow: An OutOfMemoryError is thrown when there is not enough memory to create the corresponding Java virtual machine stack during the creation/dynamic extensionCopy the code


2-3. Java virtual machine stack local method stack
The current thread is private and the Native method stack supports the call to HotSpot VM to merge the local method stack with the Java virtual machine stackCopy the code

For details on thread private and interthread sharing, see the Java Memory Model (JMM).


2-4.Methods area
- XX: PermSize = - XX: MaxPermSize = -- -- -- - > [1. Basic introduction] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current thread Shared area: It is used to store class information that has been loaded by the virtual machine, final constants, static variables, code compiled by the compiler on the spot, and so on. -- -- -- -- > runtime constant pool [2] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | - used to store the compile-time generates the literal constants, symbols referenced, translate directly | - storage at runtime constants (such as intern method of the String class is String maintains a constant pool, if the call of the character "ABC" has been in constant pool, the String is returned pool address, otherwise, create a new constant to join in the pool, [symbol reference]: encoding is a string representing the location of a variable, interface, direct reference is the translation of the address according to the symbol reference. Will be completed in class link phase translation -- -- -- - > [3. Abnormal related] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- anomalies: an OutOfMemoryError.Copy the code


Introduction to method zones, permanent bands, and meta-spaces

Method Area: a component (interface level) of the runtime data Area in the JVM specification (Perm) : included in JDK7 and earlier versions, is the implementation of the Method Area. Metaspace: in jdk8 and later, Metaspace is a concrete implementation of a method area. (Implementation level)Meta space uses local memory and is not in the JVMIt's not that hard to understand: define and use the interface first, then use the concrete implementation to get the job done.Copy the code

2-5.The Java heap

The Java heap: To store objects | - Cenozoic: store the new object | - Eden (E) : just generated Java objects stored in the JVM | - Survivor (S) | - FromSpace (S0) | -- - ToSpace (S1) | - old s: storage long-lived objects | - age standard (the number of new generation survive in GC), by - XX: MaxTenuringThreshold specified | - large objects, directly into old age. Non-heap: Stores objects that are long-lived while the program is running, such as class metadata, methods, constants, attributes, and so on.Copy the code

-xx :NewSize= Cenozoic era size -xx :SurvivorRatio= Ratio of Zone E to two ZONES S Default 8:2 -xx :NewRatio= ratio of Cenozoic era to old age -xx :MaxTenuringThreshold Age threshold for entering old age -xms Initial heap size: defaults to 1/64 of physical memory -xmx Maximum heap size: defaults to 1/4 of physical memory -xx :PermSize= Non-heap memory Initial value: defaults to 1/64 of physical memory -xx :MaxPermSize= Maximum non-heap memory: The default is 1/4 heap size of physical memory: New generation + old generation + persistent generationCopy the code

Heap and garbage collection mechanism

Full GC: Cleans up the entire heap spaceCopy the code

1: MinorGC
Generally, newly created objects enter E area. When E area memory is used up, MinorGC is triggered. Surviving objects end up in a SurvivorFromCopy the code


2. Two zones of SurvivorFromandTo
1. When E is full again 2. MinorGC collection is triggered 3Copy the code


3. A few small questions:
[1]. Why is there a new generation and an old age? | - the plight of the object is different, use different recovery algorithm, optimization of GC performance | - in Cenozoic objects may be created or destroyed frequently (towards the life in death), object recycling old s less [2]. The Survivor area exist? Improve the threshold for objects to enter the old age and reduce the frequency of FullGC (FullGC is time-consuming) [3]. What are the functions of S0 and S1? Avoid fragmentation of Survivor spaceCopy the code

Fourth, garbage collection algorithm

Garbage objects to determine | -- - | reference counting method - accessibility analysis recovery algorithm | - tag removal | | - replication - tag finishing | - a generational garbage collector collection: | - Serial | - Parnew | - CMS | - G1Copy the code

1. Determination of garbage objects
1-1. Reference counting
Add a reference counter to an object, and when the object is referenced, the reference counter +1 and when the reference is invalidated, the counter -1 object cyclic reference is invalidatedCopy the code

1-1. Accessibility analysis

GC Root in Java includes:

[1]. Objects referenced in the virtual machine stack. [2]. Objects referenced by class static properties in the method area. [3]. Objects referenced by constants in the method area. [4]. Native method stack (JNI) is generally referred to as Native reference object.Copy the code

If there is no link between an object and GC Root, the object is recyclable such as ObjC and ObjB are disconnected, and objects in orange are recyclable


2. Recycling algorithm
2-1: Mark-sweep

---->[method]---------------------------------- [1]. Mark all objects that need to survive [2]. After the mark is completed, unmarked objects are uniformly recycled. ---->[disadvantages]---------------------------------- [1]. Labeling and scavenging are inefficient. [2]. A large number of discrete memory fragments are generated. Disadvantages of large space fragmentation: When a program needs to allocate memory for large objects, it cannot find enough contiguous memory to start GC.Copy the code

2-2:Mark-compact

---->[method]---------------------------------- [1]. Mark all live objects [2]. Let all live objects are moving to the end, in the process of moving to remove unlabelled object -- -- -- - > [quality] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- best: won't produce a large number of discrete pieces of memory problems Bad: With more replication operations to perform, the efficiency becomes low and is not suitable for high survival situationsCopy the code

2-3: Copy algorithm

---->[method]---------------------------------- [1]. Divide the available memory into two equally sized pieces by capacity and use only one piece at a time. [2]. Recycling, will live in the memory object is copied to another block of memory, and then remove the memory space -- - > [quality] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- best: no space debris, simple implementation, run efficient carrier: lower memory can be used as a halfCopy the code

2-4: Collection by generation

It’s not an algorithm, it’s an algorithm for different generations of when

New generation: more recyclable objects, high recovery rate. | - replication algorithm, high efficiency, no debris, from E to S area. Old age: less recyclable objects, low recovery rate. | tags - sorting algorithm, without fragments.Copy the code

3. Introduction to garbage collector
---->[Next-generation recycler: Minor GC] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - [1]. Serial (Serial GC) - copy [2]. ParNew Parallel (GC) - copy [3]. The Parallel Scavenge (GC) Parallel recovery - copy ---->[Old age recycler: Full GC] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - [4]. Serial Old (MSC) (Serial GC) - tag - finishing [5]. CMS (concurrent GC) - tag - clear [6]. The Parallel Old (GC) Parallel - tag - finishing -- -- -- - > [G1 independently] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - [7]. The G1 (JDK1.7 +)Copy the code

Well, that’s all for the overview, and the details can be found in the following topics.