Class loader

The ClassLoader is responsible for loading the class file, and only the class file. Whether or not it can run is determined by the Execution Engine.

Start the class loader: the class loader is responsible for loading the classes stored in the

\lib directory, or in the path specified by the -xbootCLASspath parameter, and the Java VIRTUAL machine can set other class libraries to load into the virtual machine’s memory.

The extension class loader is responsible for loading all libraries in the

\lib\ext directory or in the path specified by the java.ext.dirs system variable.

Application class loader: Loads all libraries on the user’s ClassParh, and developers can also use this class loader directly in their code. This is the default class loader if the application has not customized its own class loader.

Custom loaders: Custom class loaders are used when we need to prevent Java code from being decompiled or loaded from non-standard sources (such as byte code in a database). The user can inherit the ClassLoader class, and the findClass method can customize the ClassLoader.

Parent delegation mechanism

If a class loader received the request of the class loading, it will not try to load this class first, but the commission this request to the parent class loader to load, so all the class loading should begin from the top load, only the parent class loader feedback cannot complete class loading request, the loader will try to load.

JVM memory region

Heap (thread sharing)

  • The Java heap is the largest area of memory managed by the Java Virtual machine, whose sole purpose is to hold instances of objects. So the heap is also the main area managed by the garbage collector. The heap can be in a physically discontinuous memory space as long as it is logically contiguous. The heap can also be subdivided into new generation and old age, among which the new generation is divided into Eden space, From Survivor space, To Survivor space, etc.

Method area (thread sharing)

  • Store virtual machine loaded class information, constants, static variables, just-in-time compiler compiled code and other data.

Java Virtual Machine Stack (thread private)

  • As each method is executed, the Java virtual machine synchronously creates a stack to store information about local variables, operand stacks, dynamic connections, method exits, and so on. The process of each method being called and executed corresponds to the process of a stack frame moving from the virtual machine stack to the virtual machine stack. Local variables hold various Java virtual machine base data types, object references, and returnAddress types that are known at compile time.

Local method stack (thread private)

  • Methods that are native are native methods, and these methods are placed in a place called the native method stack.

Program counter (thread private)

  • Is a small memory space that can be thought of as a line number indicator of the bytecode being executed by the current thread.

How do I determine whether an object can be reclaimed

Reference counting algorithm

Add a counter to an object. Whenever a reference is made, the value of the counter increases by one. When the reference is invalid, the value of the counter decreases by one. However, one drawback of this scheme is that it cannot solve the problem of circular references (for example, A refers to B, and then B refers to A), which makes garbage uncollected.

Accessibility analysis algorithm

The basic idea of the algorithm is as follows: start from the root object of GCRoots as the starting point, and search down according to the reference relationship from the starting point. If there is no reference chain between an object and GCRoots, it can be proved that the object is not in use.

Objects that can be used as GC Roots include:

  • Objects referenced in the virtual machine stack, such as parameters, local variables, temporary variables, and so on used in the method stack called by individual threads.

  • Objects referenced by class static properties in a method area, such as reference static variables.

  • An object referenced by a constant in a method area, such as a reference in a string constant pool.

  • Objects referenced by JNI (Native methods) in the Native method stack.

  • Internal references to the Java virtual machine, such as Class objects for basic data types, some resident exception objects, and system loaders.

  • All objects held by the synchronized keyword

  • Jmxbeans that reflect Java virtual machine internals, callbacks registered in JVMTI, native code caches, and so on.

JVM garbage collection algorithm

Mark-clear algorithm

The two steps of the algorithm are “marking” and “cleaning” respectively. The marking process is a judgment process to judge whether the object belongs to garbage or not, and it will be cleaned up uniformly after the marking is completed.

Disadvantages: Execution efficiency is inconsistent. If the heap contains a large number of objects, this time will require a lot of marking and clear operation, resulting in more objects will be less efficient.

A large number of fragments of discontinuous memory space will be generated after the mark is cleared. When the space fragment is too large, the program cannot find the continuous space that meets the condition when allocating large objects and has to trigger a garbage collection action in advance.

Replication algorithm

It will be available memory according to the capacity divided into two equal two areas, each time use one of them, when the space is used up, it will be copied to another area of the surviving objects, and then use the space after a one-time clean up. If most of the objects in memory are alive, it will take a lot of time to copy between memory, so this algorithm is suitable for regions with a few alive objects. When allocating memory in this way, you don’t have to worry about memory fragmentation, just move the heaptop pointer. According to the order of distribution.

Disadvantages: The replication algorithm reduces the available memory space by half.

Mark-tidy

The mark-up algorithm moves the markup survivable object to one end of the memory space and then cleans up the memory outside the boundary.

Generational collection algorithm

According to the life cycle of the object, different garbage collection algorithms are selected according to the different characteristics of the new generation and the old age object. For example, the new generation adopts the replication algorithm, and the old age uses the tag clearing or tag sorting algorithm.

Collector type

Serial collector

Is the most basic and oldest collector, a single-threaded collector. All other worker threads must be suspended while garbage collection is in progress until the collection is complete.

                                       

ParNew collector

This is essentially the multithreaded parallel version of the Serial collector.

                          

Parallel avenge

The new generation of collectors, also collectors of replication algorithms, are parallel multithreaded collectors whose goal is to achieve a manageable throughput. Throughput is the ratio of the CPU time spent running user code to the total CPU consumption. For example, if the virtual machine runs for 100 minutes and garbage collection takes 1 minute, the throughput is 99%.

The Parallel Scavenge collector provides two parameters for precise throughput control, the -xx :MaxGCPauseMillis parameter, which controls the maximum garbage collection pause time, and the -xx :GCTimeRatio parameter, which directly sets the throughput size.

Serial Old collector

An older version of the Serial collector, also a single-threaded collector, uses a mark-collation algorithm.

Parallel Old collector

An older version of the Parallel Old collector, using multithreading and mark-collation algorithms.

CMS collector

CMS is an old collector, is a collector to obtain the shortest collection pause time for the goal, the CMS collector is based on

Implemented by the mark-sweep algorithm, the CMS collector’s memory collection process is generally executed concurrently with the user thread. Note that the CMS does not work with the Parallel Scavenge collector.

The flow of the CMS collector is divided into the following four steps:

Initial marking: This stage marks only the following objects that GC Roots can be directly associated with, for a very short time.

Concurrent marking: This stage is the process of traversing the entire object graph from the directly associated object of GC Roots, which is run concurrently with the user thread.

Relabelling: This phase corrects the marking record of the part of the object that changes the marking as the user program continues to operate during concurrent marking. This phase is usually longer than the initial marking and shorter than the concurrent marking.

Concurrent cleanup: This phase of cleanup removes marked dead objects and is also performed concurrently with the user thread.

CMS is a very good garbage collector, but there are three drawbacks as shown below:

  • The CMS collector is very CPU sensitive;

  • The CMS collector cannot handle floating garbage;

  • CMS is a mark-clean collector that generates a lot of space debris, but the CMS collector provides a parameter -xx :+UseCMS -compactatFullCollection switch parameter (enabled by default, but deprecated by the JDK). When the CMS collector has to perform a FullGC process to open a defragmentation merge process, this eliminates the fragmentation problem but causes a longer pause, so the design compiles a new parameter — XX: cmsfullgcsbefore-compaction. The purpose of this parameter is to require the CMS collector to defragment before the next Full GC (the default is 0, which means defragmenting each Full GC) after several Full GC runs.

G1 collector

Although G1 is still designed according to the generation theory, it no longer insists on a fixed size and a fixed number of generation regions. Instead, G1 divides the continuous heap into multiple independent regions of equal size, and each region can play the role of Eden region, Survivor region, or old chronological space as required. The collector can handle different policies based on the area of the role.

A Region has a special Humongous Region that is used to store large files. G1 considers an object that is more than half of the size of a Region to be a large object. The size of each Region can be determined by -xx: G1HeapRegionSize the value ranges from 1MB to 32MB. Large objects are stored in N consecutive humongousregions. Most of G1’s behavior treats the Humongous Region as part of the old age.

The G1 garbage collector runs the procedure

Initial tagging: Simply tagging an object that GC Roots can be directly associated with, this phase requires the thread to be stopped, but this is very short and is done synchronously with the Minor GC, so there are no additional pauses.

Concurrent marking: Start with GC Roots to analyze the reacability of objects in the heap and recursively scan the object graph in the whole heap to find objects to be reclaimed. This stage is time-consuming, but since it is concurrent, it can be carried out at the same time with the user program without stopping.

Final mark: Another short pause is made on the user thread to deal with the last few SATB records that remain after the end of the concurrent phase

Filter recycling: You can update the statistics of a Region, sort the reclamation value and cost of each Region, customize the reclamation plan based on the expected pause time of users, select multiple regions to collect data, and copy the surviving objects of the Region to an empty Region. Then clean up the entire space of the old Region. The movement of live objects is involved, and the user thread must be paused, and multiple collector threads are performed in parallel.

Can act as an understanding garbage collector

Shenandoah collector: concurrent markup, concurrent collection, and concurrent reference updates.

ZGC collector: concurrent marking, concurrent prep reallocation, concurrent reallocation, concurrent remapping.

For details about this chapter, see Understanding the JVM VM.

If you think this article is helpful to you, please give the author a point of concern, there will be dry goods output every week!