What is the JVM
The JVM, short for Java Virtual Machine (Java Virtual Machine), is an imaginary computer that shields information relevant to a particular operating system platform, allowing Java programs to generate only object code (bytecode, ByteCode), can run unmodified on a variety of platforms. Behind this is the JVM’s ability to translate bytecode into platform-specific machine instructions, enabling “Write Once, Run Anywhere.” Why is Java cross-platform? Java introduced the concept of bytecodes, which the JVM can only understand and interpret into API calls to the system. There are different JVM implementations for different systems, some for Linux and some for Windows, but the bytecodes for the same code are the same when compiled. Running on different platforms means that bytecode is interpreted as machine code for different platforms by the JAVA interpreter, which maps to API calls for different systems on different JVM implementations, allowing code to run unmodified across platforms.
Relationship between JVM, JRE, and JDK
- The Java Runtime Environment (JRE) is designed for users of Java programs rather than developers. JRE is the collection of environments necessary to run Java programs, including the JVM standard implementation and Java core class libraries. It includes the Java Virtual Machine, Java platform core classes, and support files
- Java Development Kit (Java Development Kit), including the Java runtime environment (JRE), and provides a bunch of Java tools tools.jar and Java standard class library (Rt.jar)
The relationship is JDK>JRE>JVM
Java VIRTUAL Machine operating principles
There are two phases: Compilation phase: When we compile a.java file, the compiler generates a.class file with the same name. The operation stage is mainly divided into the following steps:
-
loading
- Gets the binary byte stream of a class by its fully qualified name
- Convert the static storage structure of this byte stream into a method area runtime data structure
- Generate a java.lang.Class object representing the Class in the memory heap as an access point to the Class data
-
The three steps of validation, preparation, and parsing can be regarded as a connection process. The purpose of validation is to connect the bytecode of the Class to the running state of the JVM. The purpose of validation is to ensure that the byte stream of the Class file contains information that meets the requirements of the current VIRTUAL machine and does not threaten the security of the JVM.
- File format verification to verify whether the byte stream conforms to Class file specifications and can be processed by the current version of the VIRTUAL machine
- Metadata validation, semantic analysis of the information described by bytecode to ensure compliance with Java language specifications
- Bytecode validation is analyzed by data flow and control flow to determine that the semantics are legitimate and logical
- Symbolic reference validation This validation occurs during the parsing phase
-
Prepare to allocate memory for the class’s static variables, initialized to the system’s initial values. For final static modified variables, the value is directly assigned to the user-defined value. Here is an example: the initial value after the preparation phase is 0 instead of 7
public static int a=7 Copy the code
-
parsing
Parsing is converting symbolic references in a constant pool to direct references (such as Pointers to physical memory addresses)
-
Initialize the
It is during the initialization phase that the JVM actually starts executing the Java code defined in the class. 1) The initialization phase is the process of executing the class constructor () methods. The class constructor () method is generated by the compiler automatically collecting the assignment action of all class variables in a class and combining statements in a static statement block (static block). 2) When initializing a class, if the parent class has not been initialized, the initialization of the parent class must be triggered first. 3) The virtual machine ensures that a class’s () methods are locked and synchronized correctly in a multithreaded environment.
JVM memory partition
-
Program counter
The program Counter Register (Progarm Counter Register) is a small memory space that can be thought of as an indicator of the bytecode line number executed by the current thread. In the JVM, a program counter is used to record where a thread’s bytecode is executed, or an instruction to be executed. The program counter has the feature of thread isolation, that is, each thread has its own independent counter when working, which does not affect each other and is a piece of thread private memory space. If a Java method is currently executing, the program counter records the Java bytecode address being executed. If the native method is being executed, the program counter is empty.
-
Java virtual machine stack
The Java virtual machine stack is a thread-private memory space that holds local variables of a method, partial results, and participates in method calls and returns.
The virtual machine stack operator uses the stack frame to store data, and the stack frame mainly contains local variable table, operand stack, dynamic link address, return address and other information. Each method call is accompanied by a stack frame operation, and the corresponding method return corresponds to the stack frame operation. StackOverFlowError: throws an exception if the requested stack depth is greater than the maximum available stack depth during a thread's calculation. OutOfMemoryError Thrown if the Java stack is extensible and there is not enough memory to support the program's expansion while it is running. ` ` `Copy the code
-
Local method stack The local method stack is similar to the Java virtual machine stack. The local method stack mainly manages calls to the local method stack, which is generally implemented by C. StackOverFlowError and OutOfMemoryError are raised just like the Java virtual machine stack
-
Methods area
The method area is an important part of Java memory and mainly stores metadata. The most important are the type information, constant pool, domain information and method information of the class.
-
Java heap The Java heap is arguably the most important part of Java runtime memory, where almost all objects and data are allocated space. The Java heap is divided into the new generation and the old generation. The new generation is used to store newly generated objects. If the objects have not been recycled for long enough, the old objects will be moved to the old generation. The New generation can be subdivided into Eden, Surivor space0 (S0 or from space) and Surivor space1 (S1 or To space). Eden holds the newly created objects, s0 and S1 survive at least one garbage collection, and so on. If the surviving object has not been retrieved by the specified age, it enters the old age. Permanent Generation: Permanent Generation. This means method section in Sun’s JVM, although some JVMS mostly don’t have this generation. The default minimum value is 16MB and the maximum value is 64MB
Garbage collection algorithm
- The Mark-sweep algorithm is divided into two phases: first, it flags all the objects that need to be reclaimed, and then it uniformly sweeps all the flagged objects when it’s done.
- Disadvantages: Space problem. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too many space fragments may cause that when the program needs to allocate large objects in the future, it cannot find enough continuous memory and has to trigger another garbage collection action in advance; - Advantages: Simple and fastCopy the code
-
Copying algorithms
It divides the available memory by capacity into two equally sized pieces, one of which is used at a time. When this area of memory is used up, the surviving objects are copied to the other area, and the used memory space is cleaned up again.
- Disadvantages: Only half memory usage
- Advantages: No fragmentation
-
The mark-compact marking process is still the same as the mark-clean algorithm, but instead of cleaning up the recyclable objects directly, the next step is to move all surviving objects to one end and then clean up memory directly beyond the end boundary.
- Generational Collection algorithms Current garbage Collection of commercial VMS uses a Generational Collection algorithm, which divides memory into several blocks based on the life cycle of an object and uses a different garbage Collection algorithm. Typically, the Java heap is divided into the new generation and the old generation, so that the most appropriate collection algorithm can be used for each generation. In the new generation, a large number of objects are found dead and only a few survive in garbage collection, so the replication algorithm is selected, and only a small amount of the replication cost of the surviving objects can be collected. In the old days, because the object has a high survival rate and there is no extra space to allocate it, it has to use the “mark-clean” or “mark-tidy” algorithm for recycling.
Garbage collector
- Serial collectorThe new generation of collectors, using the stop copy algorithm, uses one thread for GC, serial, and other worker threads to suspend.
- ParNew collector
The new generation collector, using the stop copy algorithm, multi-threaded version of Serial collector, uses multiple threads for GC, parallel, other worker threads pause, and focuses on reducing garbage collection time.Copy the code
- The Parallel Scavenge collector, a new generation collector that uses a stop copying algorithm and focuses on CPU throughput, the time/total time a user’s code is run, e.g. The JVM runs for 100 minutes, including 99 minutes of user code and 1 minute of garbage collection, with a throughput of 99%. This type of collector is the most efficient use of the CPU and is suitable for running background operations (collectors that focus on reducing garbage collection time, such as CMS, have little waiting time, so it is suitable for user interaction and improves the user experience).
- Serial Old collector
Old s collector, single thread collector, serial port, use the tag finishing (sorting method is to Sweep (cleaning) and Compact (compression), clean up is eliminated abandoned objects, only the surviving object, compression is moving objects, the space filling to ensure memory is divided into 2 pieces, a piece of all objects, a free) algorithm, using single thread for GC, The Serial Old collector was used with the ParallelScavenge avenge prior to JDK1.5.
- Parallel Old collector
The older collector, multithreaded, Parallel insane, uses the tag avenge (as opposed to the Serial Old, where the avenge is Summary and Compact, and the avenge is the copying of surviving objects into a pre-prepared area, Rather than Sweep discarded objects like the Sweep algorithm, Parallel Old still needs to suspend other threads while it executes. Parallel Old is useful in multi-core computing. The Parallel Avenge collector is insane and output-first. The Parallel Avenge collector is insane.Copy the code
- CMS (Concurrent Mark sweep) collector
The old collector, which aims to obtain the shortest collection pause time (i.e., shorten the garbage collection time), uses the tag sweep algorithm, multi-threaded, and has the advantage of concurrent collection (the user thread can work at the same time as the GC thread), with small pauses. -xx :+UseConcMarkSweepGC is used for ParNew+CMS+Serial Old for memory reclamation. ParNew+CMS is preferred (for reasons listed below). If the user thread memory is insufficient, Serial Old is used as an alternative.Copy the code
- G1 collector
1. In the initial marking stage, we only Mark objects that GC Roots can be directly associated with, and modify the value of TAMS (Next Top at Mark Start) so that new objects can be created in the correct Region when the user program runs concurrently in the Next stage. In this stage, we need to stop the thread. But it takes very little time. 2. The concurrent marking phase starts from GC Root to analyze the reachability of objects in the heap to find the surviving objects. This phase is time-consuming, but can be executed concurrently with user programs. 3. The final tagging phase is used to correct the part of the tagging record that changed during the concurrent tagging because the user program continued to operate. The virtual machine records the changes in the thread Remembered Set Logs during this time. The final marking phase requires the consolidation of data from the Remembered Set Logs into the Remembered Set. This phase requires the thread to be paused, but can be performed in parallel. 4. Finally, in the screening recovery stage, the value and cost of each Region are sorted first, and the recovery plan is made according to the GC pause time expected by users. According to the information revealed by Sun Company, this stage can also be executed concurrently with user programs, but because only part of regions are recovered, Time is user-controlled, and pausing the user thread greatly improves collection efficiency. The following figure clearly shows the concurrency and pause phases of the G1 collector's operational steps.Copy the code