I’m going to talk about the JVM in terms of runtime data fields, class loading mechanisms, class loaders, garbage collectors, garbage collection algorithms, JVM heap memory model, JVM memory structure, and JVM tuning.
The run-time data area
When executing a Java program, the Java virtual machine divides the memory it manages into several different data areas, each of which has a different role. These areas are collectively called “runtime data areas”. So let’s look at a picture of these areas.1. Program counter
1> Small memory space.
2> Line number indicator of the current thread bytecode.
3> Change the value of the counter to select the next bytecode instruction to execute.
4> A processor executes instructions in only one thread, one counter per thread in order for the thread to switch back to the correct execution position.
2. Java Virtual machine stack
1> Thread private.
2> The life cycle is the same as the thread.
3> Each method execution creates a stack frame for storing information about local variables, operand stacks, dynamic links, method exits, and so on.
3. Local method stack
1> Thread private.
2> Native method services used.
4, the Java heap
1> The largest block of memory.
2> Thread sharing.
3> Overflow throws OutOfMemoryError.
5. Method area
1> Thread sharing.
2> Used to store data such as type information, constants, static variables, and code cache compiled by the timely compiler that has been loaded by the virtual machine.
6. Runtime constant pool (subregions within method areas)
1> The runtime constant pool is part of the method area.
2> Store various literal and symbolic references.
Second, garbage collection algorithm
1. Mark-clear algorithm
All marked objects are marked first, and all marked objects are recycled after marking is complete. As shown in figure.
2. Mark-copy algorithm
Divide the available memory into two equally sized pieces by capacity and use only one piece at a time. When the memory in this area is used up, the surviving objects are copied to the other area, and the used memory space is cleaned up again. As shown in figure.
3. Mark-tidy algorithm
Mark-clean algorithm is the mark-clean algorithm and then the living objects sorted together to make more contiguous space. As shown in figure.
Garbage collector
1. Serial collector
The Serial collector, the most basic and oldest, is a single-threaded collector.
ParNew collector
The ParNew collector is essentially a multithreaded parallel version of the Serial collector that can use multiple threads simultaneously for garbage collection.
Parallel avenge
The Parallel Collector is a new generation collector based on the mark-copy algorithm. The goal of the Parallel Scavenge is to achieve a controlled throughput, which is the ratio of the amount of time the processor spends running user code to the total amount of time the processor consumes, i.e.
Serial Old collector
Serial Old is an older version of the Serial collector, which is also a single-threaded collector.
Parallel Old collector
The Parallel Old collector is an older version of the Parallel Avenge collector that supports multi-threaded Parallel collection and is based on the mark-collation algorithm.
6. CMS collector
The CMS collector is a collector whose goal is to obtain the shortest recovery pause time. Its operation process is divided into four steps, including:
1> Initial tag
2> Concurrent flags
3> relabel
4> Concurrent cleanup
Initial tag: The initial tag needs STW, and the initial tag simply marks objects that GC Roots can be directly associated with, which is fast.
Concurrent marking: The concurrent marking phase is the process of traversing the entire object graph from the directly associated objects of GC Roots. This process is time-consuming but does not require the user thread to be paused and can be run concurrently with the garbage collection thread.
Relabelling: The relabelling phase corrects the marking record of the part of the object that is marked as the user program continues to operate during the concurrent marking phase. The pause time of this phase is usually slightly longer than that of the initial marking phase, but much shorter than that of the concurrent marking phase.
Concurrent cleanup: This phase of cleanup removes dead objects judged by the marking phase. Since no live objects need to be moved, this phase can also be concurrent with the user thread.
Garbage First collector (G1)
G1 no longer insists on a fixed size and a fixed number of generational regions, but divides the continuous Java heap into multiple independent regions of equal size. Each Region can act as the Eden space of the new generation, Survivor space, or old chronospace as required. As shown in figure.
The OPERATION of the G1 collector can be roughly divided into four steps:
Initial tag: Just mark the objects that GC Roots can be directly associated with,
Concurrent marking: Reachability analysis of objects in the heap is performed starting with GC Root, recursively scanning the entire heap object graph for objects to reclaim. This phase is time-consuming, but can be performed concurrently with user programs.
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, and make a reclamation plan based on the pause time expected by users. You can select multiple regions to form a collection and copy the surviving objects of the Region to an empty Region. Then clean up the entire old Region. The operation here is designed to move live objects in such a way that the user thread must be paused, and multiple collector threads are done in parallel.
Class loading mechanism
When we run a class, we call the JVM.dll file through java.exe, create a class loader, and then load our class, which goes through loading, validation, preparation, parsing, and initialization.
Load: This stage loads the binary byte stream of the class into memory.
Verification: Verifies file format, metadata verification, bytecode verification, and symbol application verification to ensure that these codes do not harm vm security after running.
Preparation: The preparation phase allocates memory for variables defined in the class (that is, static variables, which are modified static) and sets the initial default values for class variables.
Parsing: The parsing phase is the process by which the Java virtual machine replaces symbolic references in the constant pool with direct references, which describe the referenced target with symbols, and direct references, which describe the applied target with addresses.
Initialization: Since the previous preparation stage is to assign the default value to the static variable, the initialization stage is to assign its real value to the static variable.
Class loaders
1. Start the class loader: This class loader is responsible for loading the jar of the system in the <JAVA_HOME>\lib directory.
2. Extension class loader: This class loader is responsible for loading the extension class JAR stored in the <JAVA_HOME>\lib\ext directory.
3. Application class loader: This class loader is responsible for loading our own classes.
6. Parental delegation model
As mentioned earlier, each class has a corresponding class loader to load the class. Different class loaders load different types of classes. When we load a class, we first load it into the collection that the application class loader has loaded. Here is loaded) in a collection of view have to load this class, if you don’t go to extend the class loader loads a collection of viewing ever loaded in this class, if not to go up again looking the bootstrap class loader to load the set look have loaded in this class, if there is no from the bootstrap class loader to load the core classes for any classes to be loaded, If not, look down in the extension classloader for the extension classes to load, and if not, look in the application loader.
Benefits:
If the Student class has been loaded by the application class loader, the Student class will not be loaded again. If the Student class has been loaded by the application class loader, the Student class will not be loaded again.
2, to prevent malicious modification of the core class library, for example, we write a String class, we go to run the class, the system will load the Java core class library in the boot loader String class, but will not load our custom String class, which prevents us from tampering with the core class library.
JVM heap memory model
In the JVM, our heap memory model is roughly as shown in the figure. Our newly generated objects will be placed in Eden. When Eden is full, we will perform a light GC and put the living objects in Eden and S0 into S1. Then, the newly generated objects will be put into Eden area. When Eden area is full again, the surviving objects in Eden area and S1 area will be put into S0 area, and then Eden area and S1 area will be emptied. The cycle repeats. If the age of the surviving object reaches 15 (this value can be adjusted), the object is put into the old age, or if the survivor region is full, some objects are put into the old age, and if the old age is full, a re-gc is performed.
JVM tuning
So-called JVM tuning mainly is to reduce the number of heavy GC heap memory, so that we will according to what object will enter old age for analysis, the first is to survive 15 generations (this value can be adjusted), this kind of situation usually are some constants, etc, the optimization of these generally do not have again big space, or some code problem caused by circulation call, This can be done by modifying the code. Secondly, when the survivor area is full, some objects will be put into the old age. At this time, we need to adjust the size of the heap memory and the ratio between the young generation and the old generation according to the business situation. Generally, for example, in some e-commerce systems, some order objects will enter the memory immediately. Orders from creation to persisted in the database does not use in the memory, so some of the normal business of the object will be back in light of GC, but there are some large objects may not put in the young generation will into old age, so this kind of situation we are about to the pile of the size of the young generation in the memory, some large objects can be placed into the young generation, And is collected by the garbage collector with the light GC.