JVM class loading and garbage collection

1. Class loading

1.1 Class loading process

Corresponding to a class, its life cycle is like this:
insert image description here
the first 5 steps are in a fixed order and are also the process of class loading, and the middle 3 steps belong to the connection, so for class loading, it is divided into the following parts steps:

  1. The " Loading
    " stage is a stage in the entire "Class Loading" (Class Loading) process. It is different from Class Loading. One is Loading and the other is Class Loading, so don't put the two are confused.
    In the Loading stage, the Java virtual machine needs to complete the following three things:
    1) Obtain the binary byte stream defining this class through the fully qualified name of a class.
    2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.
    3) Generate a java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.
  2. Connection
    1. Verification
    Verification is the first step in the connection phase. The purpose of this phase is to ensure that the information contained in the byte stream of the Class file meets all the constraints of the "Java Virtual Machine Specification" and that the information is run as code. It will not endanger the security of the virtual machine itself.
    What needs to be verified are: file format, verification of bytecode verification, symbol reference verification...
    2. Preparation
    The preparation stage is to formally allocate memory for the variables defined in the class (that is, static variables, variables modified by static) and set the initial value of class variables stage. (Initialize the variable to 0, and assign it to the initial value given in the code during the initialization phase)
    3. Resolution
    The resolution phase is the process in which the Java virtual machine replaces symbolic references in the constant pool with direct references, that is, the process of initializing constants.
    Before the class is loaded, the string constant is saved in the .class file. At this time, the reference records the offset in the file, not the real address of the string. The string constant is actually put into the memory after the class is loaded , this reference can actually be assigned to a memory address.
  3. Initialization In the initialization
    phase, the Java virtual machine actually starts to execute the Java program code written in the class, and transfers the dominance to the application program. The initialization phase is the process of executing the class constructor method.

1.2 Parental delegation model

What is the parent delegation model?
If a class loader receives a class loading request, it will not try to load the class first, but will delegate the request to the parent class loader to complete, and each level of class loader This is the case, so all loading requests should eventually be delivered to the top-level startup class loader, and only when the parent loader reports that it cannot complete the load request (it does not find the required class in its search scope), the child The loader will try to complete the loading by itself.
insert image description here

Advantages of the Parental Delegation Model

  1. Avoid repeated loading of classes: For example, class A and class B both have a parent class C, then class C will be loaded when A starts, then there is no need to repeatedly load class C when class B is loaded.
  2. Security: Using the parental delegation model can also guarantee the core API type of Java, but if each class loader loads itself, there will be some problems. For example, if we write a class called java.lang.Object, then the program runs Sometimes, there will be many different Object classes in the system, and some Object classes are provided by users themselves, so the security cannot be guaranteed.

2. Garbage collection mechanism

Almost all object instances are stored in the Java heap. Before the garbage collector performs garbage collection on the heap, it must first determine which of these objects are still alive and which have "dead". There are several algorithms for judging whether an object is "dead":

2.1 Judgment algorithm for dead objects

  1. Reference counting method
    The algorithm described by reference counting is:
    add a reference counter to the object, whenever there is a place to refer to it, the counter will be +1; If it is used again, the object is "dead".
    The reference counting method is simple to implement, and the judgment efficiency is relatively high. It is a good algorithm in most cases. For example, the Python language uses reference counting for memory management.
    However, the reference counting method is not used to manage memory in the mainstream JVM. The main reason is that the reference counting method cannot solve the circular reference problem of objects,
    as shown in the figure below:
    insert image description here
    a and b respectively create an instance of an object, and at this time two The reference count of the object is 1, and then the reference count of the two variables pointing to each other becomes 2, but when the two variables are released, the reference count -1 becomes 1, but at this time the two variables are useless Yes, since the reference count is still 1 and will not be recycled, it will cause garbage collection to be delayed.

  2. Reachability analysis algorithm
    As we said above, Java does not use the reference counting method to judge whether the object is "dead", but uses "reachability analysis" to judge whether the object is alive (this method is also used by C#, Lisp - the first language with dynamic memory allocation). The core idea of ​​this algorithm is: use a series of objects called "GC Roots" as the starting point, start searching downward from these nodes, and the path traveled by the search is called "reference chain". When an object reaches GC Roots without When any reference chain is connected (from GC Roots to this object is unreachable), it proves that this object is not available. The following figure is an example:
    insert image description here
    e This object is not reachable from the root, so it will be regarded as garbage and collected.

In the Java language, objects that can be used as GC Roots include the following:

  1. Objects referenced in the virtual machine stack (local variable table in the stack frame);
  2. Objects referenced by class static properties in the method area;
  3. Objects referenced by constants in the method area;
  4. The object referenced by JNI (Native method) in the native method stack.

From the above, we can see the function of "reference". In addition to the earliest we used it (reference) to find objects, now we can also use "reference" to judge dead objects. Therefore, in JDK1.2, Java expanded the concept of references and divided references into four types: strong references, soft references, weak references, and phantom references. The strengths of the four citations are in descending order.

2.2 Garbage collection algorithm

Through the above learning, we can mark the dead objects. After marking, we can perform garbage collection operations. Before formally learning the garbage collector, let's look at several algorithms used by garbage collection machines (these algorithms are garbage The guiding ideology of the collector).

  1. The "mark- and
    -sweep" algorithm is the most basic collection algorithm. The algorithm is divided into two stages: "marking" and "clearing": as shown in the figure below:
    insert image description here
    first mark all objects that need to be recycled, and then recycle all marked objects uniformly after the marking is completed.
    There are two important problems in this method:

    1. Efficiency issues: Marking and clearing are both inefficient processes
    2. Space problem: A large number of discontinuous memory fragments will be generated after the mark is cleared. Too much space fragmentation may cause that when the program needs to allocate large objects in the future, it will not be able to find enough continuous memory and have to trigger another garbage collection in advance.
  2. Copy Algorithm
    The "copy" algorithm is to solve the efficiency problem of "mark-clean". It divides the available memory into two pieces of equal size according to the capacity, and only uses one of them at a time. When this piece of memory needs to be garbage collected, the surviving objects in this area will be copied to another piece, and then the used memory area will be cleaned up at one time. The advantage of this is that the entire half area is reclaimed every time, and there is no need to consider complex situations such as memory fragmentation when allocating memory. You only need to move the top pointer of the heap and allocate in order. This algorithm is simple to implement and efficient to run. The execution flow of the algorithm is as follows:
    insert image description here

insert image description here

  1. Generational Algorithm
    The generational algorithm implements different regions and different garbage collection strategies through regional division, so as to achieve better garbage collection. This is just like China's one country, two systems policy. It is more in line with local rules for different situations and regional settings, so as to achieve better management. This is the design idea of ​​the time-generation algorithm. As shown in the figure below: When a variable is created, it first exists
    insert image description here
    in In the Eden area, after the GC still exists, it will go to the new area and then use the copy algorithm to move back and forth between the two new area. When the GC that has experienced more times still exists, it will be transferred to the old area. Based on experience, it can be judged that the long-term existence Things will exist for a long time afterwards, so the number of GCs in the old area will be reduced and the mark-sort algorithm will be used to recycle garbage.

Guess you like

Origin blog.csdn.net/m0_71645055/article/details/132197510