jvm and GC mechanism

JVM is divided into three main subsystems

(1) a class loader subsystem (2) the runtime data area (3) execution engine

  1. Class loader subsystem

Java dynamic class loading feature is handled by the class loader subsystem. When it runs for the first time in reference to a class (not compile time), it is loaded, link and initialize the class file.

1.1 Load

Whereby the components are loaded class. Start class loader (BootStrap class Loader), extended class loader (Extension class Loader) and application class loader (Application class Loader) These three types of help loader loaded class.

  1. Start class loader - responsible for loading classes from the boot class path, nothing more than rt.jar. The loader will be given the highest priority.

  2. Extension class loader - responsible for loading ext directory (jre \ lib) in the class.

  3. Application class loader - responsible for loading the application level class path, the path comes to the environment variables, etc. etc.

Above will follow the class loader loads the class file hierarchy commissioned algorithm (Delegation Hierarchy Algorithm).
1.2 Links

  1. Check - bytecode verifier verifies the generated bytecode is correct. If the check fails, we'll get a parity error.

  2. Preparation - allocate memory and initialize the default values ​​for all static variables.

  3. Analytical - All reference symbols refer to the original memory area is method (Method Area) is replaced.

1.3 Initialization

This is the final phase of class loading, where all the static variables will be assigned an initial value, and a static block will be executed.
2. runtime data area (Runtime Data Area)

The runtime data area is divided into five main components:

2.1 Method Area (Method Area)

All class-level data will be stored here, including static variables. Each JVM is only one way area, it is a shared resource.

2.2 heap (Heap Area)

All objects and their corresponding instance variables and arrays will be stored here. Each JVM is also only a heap area. Since the method area and heap memory area shared by multiple threads, so the data is not thread safe storage.

2.3 stack area (Stack Area)

Creates a runtime stack for each thread individually. Each function will call for a memory stack frame generated in the stack (Stack Frame). All local variables are created in the stack memory. Stack area is thread-safe, because it is not a shared resource. Stack frame is divided into three fruiting bodies:

a local variable array - the number associated with the method comprising local variables and the corresponding value will be stored here.

b operand stack - you need to perform any intermediate operations, the operand stack as a work area to execute the runtime instruction.

c frame data - method all the symbols are stored here. In any abnormal situation, the catch block information will be stored in the frame data inside.
As is the JVM three core areas

2.4 PC register

Each thread has a separate PC register to store the address of the currently executing instruction, once the instruction is executed, pc register will be updated to address the next instruction.

2.5 native method stacks

Native method stacks save a local method information. For each thread will create a separate native method stacks.
3. Execution Engine

Assigned to the runtime data area of ​​the byte code executed by the execution engine. The execution engine reads the bytecode and performing piecewise.

3.1 interpreter:

Interpreter bytecode interpretation quickly, but the implementation is very slow. The disadvantage is that the interpreter, when a method is called multiple times, each time need to re-interpretation.

translater

JIT compiler eliminates the disadvantages of the interpreter. Execution engine using an interpreter bytecode conversion, but if a duplicate code using a JIT compiler to compile the entire bytecode to native code. Native code directly for repeating the method call, which improves the performance of the system.

. A intermediate code generator - generating intermediate code

b code optimizer - responsible for optimizing the intermediate code generated above

. C target code generator - is responsible for generating machine code or native code

d detectors (Profiler) -. a particular component, is responsible for finding the method is called multiple times.

3.3 garbage collector:

Collect and remove unreferenced objects. Garbage collection can be triggered by a call to "System.gc ()", but does not guarantee does garbage collection. JVM's garbage collection which objects created by the new keyword collection only. So, if it is not used to create new objects that you can use to perform cleanup finalize function.

Java Native Interface (JNI): JNI will interact with the native method libraries and provide the required execution engine local library.

Native method libraries: it is a collection of native libraries required engine performs.
Know JVM through a small program

package com.spark.jvm;
/**
 * 从JVM调用的角度分析java程序堆内存空间的使用:
 * 当JVM进程启动的时候,会从类加载路径中找到包含main方法的入口类HelloJVM
 * 找到HelloJVM会直接读取该文件中的二进制数据,并且把该类的信息放到运行时的Method内存区域中。
 * 然后会定位到HelloJVM中的main方法的字节码中,并开始执行Main方法中的指令
 * 此时会创建Student实例对象,并且使用student来引用该对象(或者说给该对象命名),其内幕如下:
 * 第一步:JVM会直接到Method区域中去查找Student类的信息,此时发现没有Student类,就通过类加载器加载该Student类文件;
 * 第二步:在JVM的Method区域中加载并找到了Student类之后会在Heap区域中为Student实例对象分配内存,
 * 并且在Student的实例对象中持有指向方法区域中的Student类的引用(内存地址);
 * 第三步:JVM实例化完成后会在当前线程中为Stack中的reference建立实际的应用关系,此时会赋值给student
 * 接下来就是调用方法
 * 在JVM中方法的调用一定是属于线程的行为,也就是说方法调用本身会发生在线程的方法调用栈:
 * 线程的方法调用栈(Method Stack Frames),每一个方法的调用就是方法调用栈中的一个Frame,
 * 该Frame包含了方法的参数,局部变量,临时数据等 student.sayHello();
 */
public class HelloJVM {
    //在JVM运行的时候会通过反射的方式到Method区域找到入口方法main
    public static void main(String[] args) {//main方法也是放在Method方法区域中的
        /**
         * student(小写的)是放在主线程中的Stack区域中的
         * Student对象实例是放在所有线程共享的Heap区域中的
         */
        Student student = new Student("spark");
        /**
         * 首先会通过student指针(或句柄)(指针就直接指向堆中的对象,句柄表明有一个中间的,student指向句柄,句柄指向对象)
         * 找Student对象,当找到该对象后会通过对象内部指向方法区域中的指针来调用具体的方法去执行任务
         */
        student.sayHello();
    }
}
 
class Student {
    // name本身作为成员是放在stack区域的但是name指向的String对象是放在Heap中
    private String name;
    public Student(String name) {
        this.name = name;
    }
    //sayHello这个方法是放在方法区中的
    public void sayHello() {
    System.out.println("Hello, this is " + this.name);
    }
}

JVM tuning three parameters: -Xms -Xmx -Xss

-Xms -Xmx is performance tuning parameters to the stack, usually two settings are the same, if not the same, when Heap is not enough memory jitter occurs. Generally turn up these two parameters, and both the same size.

-Xss is the depth of tuning parameters for each thread stack, the impact of the call stack

OOM derived from an actual demonstration of the memory structure based on the time JVM GC: Young Generation (Eden, From, To), OldGeneration, Permanent Generation

JVMHeap area (the young generation, years old) and a method area (permanent generation) structure:

Interpretation of the code from the Java GC's point of view: the program line 20 will be the first new Person object will enter the young generation in Eden (if the object is too large may go directly to the old generation). GC is the object before Eden and from the presence of, for GC of Eden when objects are copied to the To survive such a space (survive (survivor) space: comprising from and to, their size is the same, also known as s1 and s2) in (there is a copy of the algorithm), From the objects (the algorithm will take into account the number of passes GC surviving) to a certain number of times (threshold (if after each GC the object still exists in survive in, GC time his Age is increased by 1, 15 will default into OldGeneration. However, the actual situation is more complex, it is possible not just to the threshold value from the region directly to Survive Old Generation area. Survive be performed when the GC for determination of objects, Survive Some space objects Age is the same, that is, after the number of GC as the sum of the same age group of such objects is greater than or equal Survive half the space, then it will enter the old Generation group of objects, the (is a dynamic adjustment))), will be copied to OldGeneration, if not to the number of times From the object will be copied to the to, after copying to save Is a valid object, and Eden From the remaining objects are invalid, and this time put the Eden From all the objects emptied. Object replication when Eden in the To enter, To probably already full, this time in Eden objects will be copied directly to the Old Generation, From the objects will go directly to the Old Generation. That is, there is a case, To is relatively small, when the first copy space on the full, direct access to the old Generation. After copying, To and From name will

U2FsdGVkX1896f2cEM1 / e6UmP2LfdNQw5wMgdu8WTLTaEFPo86ojgPMDiXlGZxU7
eQ5d 5QlIO6VQcwny2TXNfoRkACuKlIv + + + ghEpYASXGsEH59oKm8Hf 6pxePZCfaK
RCH8lNkK9TUuyHzvIhjZoEQWDgxwhbMWedz9NXKJxkvTapwpM2DRqtlaajEV / You
hs7OfKcFXB19vW gi4tVfw == +

Swap it, since Eden and From are empty, and after Eden To swap are empty, the next assignment will be assigned to Eden. This process has been circulating. Benefits: Work with Objects and most efficient

Published 57 original articles · won praise 2 · Views 8052

Guess you like

Origin blog.csdn.net/xmh_sxh_1314/article/details/104258434