JVM, about the basic knowledge of JVM, are you sure you don't know it?

Table of contents

1. The concept of JVM

What are JVMs?

Two. JVM running process

1. How the class file is loaded and run by the JVM

2. Which areas are included in the JVM runtime data (M)

3. The process of class loading (M)

4. Parental delegation model

1. Analysis of parental delegation model

2. What class loaders are there in JAVA (M)

5. Garbage collection mechanism

1. Identification of dead objects

① Reference counting algorithm

②Accessibility analysis algorithm

 What are GC Roots?

GC Roots generally have the following types:

2. Garbage collection algorithm

①Mark removal algorithm

②Mark copy algorithm

③ Marking Algorithm

 The process of garbage collection? (M)

1. Minor GC (garbage collection of the new generation)

 2.Full GC (Garbage Collection in the Old Age)

3. Garbage collector

Introduce the Garbage Collector (M)

 The main garbage collectors are as follows:


1. The concept of JVM

What are JVMs?


JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer . 

Two. JVM running process

1. How the class file is loaded and run by the JVM

①When a .java file is compiled into a .class file, the .class file will be loaded into the class loading subsystem, and then the class loading subsystem will load the file into the runtime data area

②In the runtime data area, the class object is loaded into the method area, so that the newly created instance object can create a new object through this class object template.

③The created instance object will be loaded into the heap

④Virtual machine stack: Each thread will open up a space in the virtual machine stack, and each time a method is called, the method will be pushed into the stack, which means that each stack stores the level of method calls

⑤Local method stack: Similarly, each thread will open up a piece of memory in the local method stack, and each time a local method is called, the local method will be loaded into the local method stack.

⑥Program counter: record the address of the instruction that the current thread runs to: Considering that the java virtual machine switches threads in turn and allocates time slices in the multi-thread mode, when the time slice allocated by a certain thread is used up However, when the current thread has not finished executing, it is necessary to use the program counter to record the address of the instruction that the current thread runs to. When the current thread is allocated to the time slice again, it will continue to execute from the current instruction.

2. Which areas are included in the JVM runtime data (M)

① Method area: Before JDK1.7, the method area was also called the permanent generation, and after 1.8 it was called the metaspace. The difference lies in the different implementation methods. The method area is where the class object is stored when it is loaded into the JVM , the class object is stored in the method area so that when an instance object needs to be created later, the instance object can be directly obtained from the class object in the method area and created. It should be noted that the method area is shared by all threads

②Heap: The created instance objects will be loaded into the heap space, and the size of the heap space can be set through parameters in the JVM: Xms10 (minimum heap memory space) is to set the heap space size, and Xmx10 (maximum heap memory space) is also set Heap space size: Usually we set these two memory parameters to the same size. We generally set both parameters to the maximum heap space that the thread may consume. If the memory space is relatively small, OOM errors may occur ( OUT OF MEMORY ERROR), once this error occurs, we can set the heap space to be larger.

③java virtual machine stack: Each thread will open up a space in the virtual machine stack. The size of the machine stack capacity is generally determined by the parameter Xss. If the stack overflows, a StackOverFlow error will be reported.

④Native method stack: Similarly, each thread will open up a piece of memory in the local method stack, and each time a local method is called, the local method will be loaded into the local method stack.

⑤Program counter: record the address of the instruction that the current thread runs to: Considering that the java virtual machine switches threads in turn and allocates time slices in multi-thread mode, when the time slice allocated by a thread is used up However, when the current thread has not finished executing, it is necessary to use the program counter to record the address of the instruction that the current thread runs to. When the current thread is allocated to the time slice again, it will continue to execute from the current instruction.

3. The process of class loading (M)

1. load

Load all .class files into the virtual machine

2. Verification

Verify the current file according to the specification of the .class file

3. Prepare

Initialize various types of values ​​to default values, int is initialized to 0, float is initialized to 0.0f. …

4. Analysis

Replace the symbolic reference of the constant pool in the java virtual machine with a direct reference , which is the process of initializing the constant pool. (For example: the string in the original constant pool is not directly referenced, (it is still a placeholder). In this process, a new string will be created and the placeholder in the constant pool will be replaced)

 

5. Initialization

The previous work is all preparatory work. This step is to actually execute the java code in the class. After this step, the real java object will be created

4. Parental delegation model

1. Analysis of parental delegation model

Different classes use different class loaders when loading:

 The strategy of its class loader is as follows: When creating a class, it first forwards upwards from applicationClassLoader to BootStrapClassLoader. BootStrapClassLoader checks whether this class exists in its own loading path, loads it if it exists, and forwards it downwards to ExtClassLoader if it does not. ExtClassLoader looks for this class in its own loading path, loads it if it exists, and forwards it down to ApplicationClassLoader if it does not, finds and loads this class in its own path

2. What class loaders are there in JAVA (M)

5. Garbage collection mechanism

1. Identification of dead objects

① Reference counting algorithm

Concept: When an object is referenced, the number of references is increased by 1. When the referenced object is 0, the object is marked as a dead object, and the object should be recycled.

The diagram is as follows:

 Process analysis: When two objects are created, two variables are defined to point to the two created objects. At this time, the references of the two objects are +1. At the same time, the instance attribute of the object is also used to point to the two created objects. At this time, The reference counts of the two newly created objects are both 2, but if these two variables are set to empty, the reference count of the two newly created objects is -1, but since both variables are empty, the two The instance attribute of the variable can no longer access these two objects, but because the reference count of these two objects has not become 0 (the instance attribute still points to the two objects), these two objects cannot be recycled, which means caused a memory leak

notes: Memory leak: Memory leak (Memory Leak) refers to the heap memory that has been dynamically allocated in the program is not released or cannot be released for some reason, resulting in a waste of system memory, resulting in serious consequences such as slowing down the running speed of the program or even system crashes .

The memory leak problem is not easy to find when the program starts to run. When the program runs for a period of time, new objects are continuously placed in the heap space and cannot be cleared in time, which will eventually cause the heap space to be full and cause the program to crash.

②Accessibility analysis algorithm

The implementation principle of the reachability analysis algorithm is as follows: search from top to bottom according to the reference relationship from GC Roots (starting node set), and the search path is called [reference chain]. If a result does not reach any root object reference chain, the object is said to be unreachable, indicating that the object is unreachable.


During garbage collection, it will first search for all root nodes [enumerate root nodes], and then search from top to bottom from these root nodes. This process needs to suspend the user thread, that is, trigger STW, and the object can be found and explained. Reserved, and if the object cannot be searched for, it will be reclaimed using the garbage collection algorithm.

 What are GC Roots?

GC Roots is also an object, and it is an object that the JVM must not recycle. When the JVM performs GC, it will first perform STW after all users reach the safepoint, suspend the threads of all users, and then enumerate the root nodes, and then Search from top to bottom.

GC Roots generally have the following types:

1. The object referenced by the static attribute in the method area is
a kind of global object. The Class object itself is difficult to be recycled, and the conditions for recycling are very strict. As long as the Class object is not recycled, the static members cannot be recycled.

2. The objects referenced by the constant pool in the method area
are also global objects, such as the string constant pool. The constant itself will not change after initialization, so it is reasonable to be GC Roots.

3. The object referenced by the stack frame local variable table in the method stack
belongs to the object in the execution context. When the thread executes the method, it will package the method into a stack frame and execute it on the stack. The local variables used in the method will be stored in the stack frame in the local variable table. As long as the method is still running and has not been popped out of the stack, it means that the object in the local variable table will still be accessed, and the GC should not recycle, so this type of object can also be used as GC Roots.

4. The objects referenced in the JNI local method stack
are essentially the same as the previous one, except that one is a variable reference in the Java method stack, and the other is a variable reference in the native method (C, C++) method stack.

5. Objects held by synchronization
locks Objects locked by synchronized can never be reclaimed. If there is a thread holding an object lock, if the GC recycles the object, the lock will become invalid.

2. Garbage collection algorithm

①Mark removal algorithm

Algorithm idea: mark the unavailable objects in all current objects, mark them in place, and then clear them

Defects: But the defects of this algorithm are also obvious: clearing unusable objects in situ will generate a large number of discontinuous free space fragments. If you need to create larger objects at this time but the current continuous free space is not satisfied , the next garbage collection needs to be triggered.

 

②Mark copy algorithm

Algorithm idea: Divide all available space into two parts, such as space 1 and space 2. After clearing unusable objects in space 1, there are a large number of fragments of discontinuous free space. The available and unavailable objects are sorted and arranged, and the space 1 is cleared as a whole after the sorting is completed, and this operation is repeated when clearing again later

Defect: The defect of this algorithm is also relatively obvious. In each garbage collection process, only half of the space can really play a role

③ Marking Algorithm

The idea of ​​the algorithm is as follows: sort all the objects after clearing some unavailable objects each time

Defect: sorting is required for each clearing, and the algorithm efficiency is relatively low

 The process of garbage collection? (M)

 Different class objects are divided into generations, and all class objects are divided into new generation and old generation. Among them, the new generation occupies 1/3 of the heap space, the old generation occupies 2/3 of the heap space, and the new generation space is still There are 1/5 survivors (survivors are divided into from and to two areas) area. Objects are processed in generations, and garbage collection is divided into two types: ①Minor GC (new generation garbage collection) ②Full GC (Major GC/Full GC occurs in the old generation garbage collection).

1. Minor GC (garbage collection of the new generation)

The garbage collection of the new generation uses the copy algorithm (because the objects in the new generation iterate frequently, an algorithm with relatively high efficiency is required. Although copy algorithm consumes a large amount of space, its efficiency is relatively high ). The process is as follows:


①When creating an object, create it directly in the Eden area. At this time, both areas of the survivor are empty (ratio of space size: Eden:from:to=8:1:1)

②When we create an object again and find that the current capacity of the Eden area is not enough to store the current object, the objects in the Eden area will be judged and processed, and the objects that are determined to be dead will be cleared, and the surviving objects will be moved to the from area in the survivor area , the "age" of the object moved to the from area +1;

③The Eden area continues to create new objects, and after clearing the unreferenced objects, all the surviving objects in the Eden area are moved to the from area, and both the Eden area and the from area are fully occupied. The surviving objects are moved to the to area, and then both the Eden area and the from area are cleared, and the age of the objects placed in the from area in the previous round is increased by 1

⑤In the next round of GC, the positions of the from area and the to area will be exchanged. At this time, the to area is empty, and the from area and Eden area will continue to participate in the next round of GC, repeating the above steps

⑥ After continuous GC, the age of the objects in the new generation reaches a certain threshold (the default threshold is 15), at this time these objects must be moved to the old generation

 Summary: Let's take a look at the entire new generation garbage processing process: in each round of GC, new objects will be created in the Eden area. When the remaining capacity of the Eden area is not enough to create new objects, all Eden The surviving objects in the area are moved to the from area. In the next round of GC, all the surviving objects in the from and Eden areas will be copied to the to area, all the from area and Eden area will be cleared, and the to area and the from area will be replaced to continue garbage collection.

 2.Full GC (Garbage Collection in the Old Age)

The garbage collection strategy of the old generation does not use the copy algorithm, but the marking algorithm. Because the objects entering the old generation are not frequently created and destroyed compared with the new generation, it is more appropriate to use the marking algorithm. Full GC is much slower than Minor GC, so in the JVM tuning process, a large part of the work is the adjustment of Full GC

3. Garbage collector

Introduce the Garbage Collector (M)

Before talking about the garbage collector, let's talk about the development process of the garbage collector: Since STW is executed during the garbage collection process, all user threads must suspend thread services during this process, so shortening the STW time is garbage The goal that the collector should pursue after completing the business requirements. At the beginning, a single-threaded garbage collector was used. With the continuous expansion of the program scale and the continuous enrichment of the program content, the single-threaded garbage collector could not meet the needs of users, and the multi-threaded garbage collector came into being. With the continuous development of the program , the time of multi-threaded STW is getting longer and longer, and a new garbage collector is started to shorten the time of STW

 The main garbage collectors are as follows:

  • Serial collector (copy algorithm)
    new generation single-threaded collector, both marking and cleaning are single-threaded, the advantage is simple and efficient. It is the default GC method at the client level, which can be -XX:+UseSerialGCspecified by force.
  • Serial Old collector (mark-collation algorithm)
    old generation single-threaded collector, the old version of the Serial collector.
  • The ParNew collector (stop-copy algorithm) 
    new generation collector can be considered as a multi-threaded version of the Serial collector, which has better performance than Serial in a multi-core CPU environment.
  • Parallel Scavenge collector (stop-copy algorithm)
    parallel collector, pursuing high throughput and efficient use of CPU. The throughput is generally 99%, and throughput = user thread time/(user thread time + GC thread time). It is suitable for background applications and other scenarios that do not require high interaction response. It is the default GC method at the server level, which can be used -XX:+UseParallelGCto force the specification and -XX:ParallelGCThreads=4to specify the number of threads.
  • Parallel Old collector (stop-copy algorithm)
    The old generation version of the Parallel Scavenge collector, a parallel collector, with throughput priority.
  • The CMS (Concurrent Mark Sweep) collector (mark-cleaning algorithm)
    has high concurrency and low pauses, and pursues the shortest GC recovery pause time. The CPU usage is relatively high, the response time is fast, and the pause time is short. Multi-core CPUs are the choice for pursuing high response time.

Guess you like

Origin blog.csdn.net/m0_65431718/article/details/130295744