Virtual Machine Series | Execution Engine and Garbage Collection

1. Execution Engine

The application program is compiled and converted into a bytecode file. The bytecode loaded into the memory space cannot be directly executed on the operating system. The execution engine is an integral part of the core of the Java virtual machine. Its function is to interpret/compile bytecode instructions into Corresponds to the local machine instructions on the system platform.

Interpreter : When the virtual machine is started, it will execute the bytecode line by line according to the predefined interpretation, and interpret the content of each bytecode file as the local machine instruction execution of the corresponding system platform;

JIT compiler : The virtual machine compiles the source code into the machine language related to the local machine platform, and finds the hot and high-frequency execution code and puts it into the metaspace, that is, the JIT cache code stored in the metaspace;

Garbage collection : For objects that do not have any references are marked as garbage, they will be recycled to release memory space.

Two, garbage object mark

1. Reference counting method

Each object stores an integer reference counter, which is used to record the number of times the object is referenced. When the object is referenced by an object, the counter is increased by 1, and when a reference is lost, the counter is decreased by 1. The reference counting algorithm is determined by judging the object. The number of references determines whether the object can be recycled as garbage.

Although the reference counting method is highly efficient, when two objects refer to each other, the two objects will never be recycled, which is a fatal flaw. So JVM does not use this marking algorithm.

2. Reachability analysis algorithm

The reachability analysis algorithm judges whether the object can be recycled based on whether the reference chain from the object to the root object is reachable;

The running program treats all reference relationship chains as a graph, and uses the GC-Roots root object object collection as the starting point, and continuously searches from each root node down to whether the objects connected by the root object collection are reachable. The search path is called It is a reference chain (Reference-Chain). If there is no reference chain from the object to GC-Roots, it means that the object is not available.

  • Objects referenced in the virtual machine stack;
  • Objects referenced by class static properties in metaspace;
  • Objects referenced by constants in the metaspace;
  • Objects referenced by Native methods in the native method stack;

Compared with the reference counting algorithm, the reachability analysis algorithm avoids the problems caused by circular references. It also has the characteristics of efficient execution. It is also the marking algorithm adopted by the JVM.

Three, garbage collection mechanism

1. Mark removal algorithm

The mark-clear algorithm is divided into two stages: mark and clear:

Marking phase: Scanning from the root object collection, marking the surviving objects; Cleaning phase: Scanning again to find unmarked objects and recycling them;

This algorithm is not efficient. Garbage collection requires suspension of the application, and a large amount of memory fragmentation will be generated. When a large memory-occupying object is allocated during subsequent program operation, there will be insufficient continuous memory, which is easy to trigger another garbage collection action.

2. Marking and sorting algorithm

The marking process of the mark sorting algorithm is similar to the mark clearing algorithm. The first stage: mark garbage objects; the second stage: move all surviving objects to one end of the memory area; the third stage: directly clean up the memory outside the boundary end, similar In the process of disk defragmentation;

The garbage collection algorithm is not efficient, and the application needs to be suspended during the object movement process, which is suitable for scenes with high object survival rates (old age).

3. Copy algorithm

The copy algorithm divides the memory into two pieces of equal size according to the capacity. Only one of them is used each time. When the used memory is used up, the surviving objects are copied to another free memory, and then used The memory space is cleaned up at once.

The algorithm is simple to implement and has high operating efficiency, but the memory space is seriously wasted. It is suitable for scenarios with low object survival rates, such as the new generation.

4. Generational collection algorithm

Almost all virtual machines on the market currently use this recycling algorithm. The generational collection algorithm uses different algorithm mechanisms according to the respective characteristics of the young and old generations. The life cycles of objects in different memory areas are also different, so different areas of the heap memory are used Different recycling strategies can improve the efficiency of garbage collection. Usually, the new generation object has a low survival rate and frequent recycling, so the replication algorithm is used; the old generation has a long life cycle and high survival rate, and the mark removal algorithm or the mark sorting algorithm is used.

Java heap memory can generally be divided into three modules: young, old and permanent, as shown in the following figure:

Cenozoic

Under normal circumstances, newly created object instances are first placed in the Cenozoic space, so the pursuit of fast recycling of garbage objects is pursued. Under normal circumstances, the Cenozoic memory is divided into an eden area and two at a ratio of 8:1:1. A survivor (survivor0, survivor1) area, most of the object instances are generated in the Eden area;

During garbage collection, the surviving objects in the eden area are copied to the S0 area, and then the eden area is cleared. When the S0 area is also full, the eden area and the surviving objects in the S0 area are copied to the S1 area, and then the eden and S0 areas are cleared, and then S0 is exchanged. The role of area and S1 area. When the S1 area cannot store the surviving objects in the eden area and S0 area, the surviving objects are directly stored and moved to the old generation area. When the old generation area is also full, a FullGC is triggered, that is, the new generation, The old generations are all recycled.

Old age

The old generation area stores some objects with a long life cycle. Object instances that survive multiple garbage collections in the young generation will be moved to the old generation area.

Fourth, the source code address

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent

Recommended reading: finishing programming system

Serial number project name GitHub address GitEE address Recommended
01 Java describes design patterns, algorithms, and data structures GitHub·click here GitEE·Click here ☆☆☆☆☆
02 Java foundation, concurrency, object-oriented, web development GitHub·click here GitEE·Click here ☆☆☆☆
03 Detailed explanation of SpringCloud microservice basic component case GitHub·click here GitEE·Click here ☆☆☆
04 SpringCloud microservice architecture actual combat comprehensive case GitHub·click here GitEE·Click here ☆☆☆☆☆
05 Getting started with SpringBoot framework basic application to advanced GitHub·click here GitEE·Click here ☆☆☆☆
06 SpringBoot framework integrates and develops common middleware GitHub·click here GitEE·Click here ☆☆☆☆☆
07 Basic case of data management, distribution, architecture design GitHub·click here GitEE·Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub·click here GitEE·Click here ☆☆☆☆☆

Guess you like

Origin blog.csdn.net/cicada_smile/article/details/108792556