Hello, today for you children to share JVM, quickly take out a small notebook to write down!
The difference between JDK, JRE, and JVM
JDK (Java Development Kit, Java development tools include the Java language, tool classes (including compiler (javac.exe), development tools (Javadoc. Exe, jar. Exe, KeyTool. exe, jconsole.exe)), and JRE (Java Runtime) The Environment). To develop Java programs, both compiling and running them, you need to install the JDK.
The JRE (Java runtime environment) contains the deployment technology, the JavaSE API (base class libraries such as Java.lang and Util jars), and the JVM. If you just deploy and run Java programs, you can actually just install the JRE. If only JRE is installed on the server: 1. All files are compiled, including JSP files, when released to the server. 2. Do not directly modify the file on the server later (because the modified file is not recompiled). The JRE does not have Java compilation and translation utility classes (Java and Javac).
The Java Virtual Machine (JVM) is used to interpret bytecode files and translate them into Machine code for different operating systems. Is the foundation for Java implementation to run across platforms.
JAVA virtual machine is using C language + assembly language development, in addition to the rest are JAVA language development.
Talk about the parts that make up the JVM
The JVM consists of two subsystems and two sub-components: the Class Loader System and the Execution Engine. The two components are Runtime Data Area and Native Interface.
-
Class Loader: loads the Class file into the Method Area of the Runtime Data Area based on the given fully qualified Class name (e.g. Java.util.scanner).
-
Execution Engine: Executes instructions in a bytecode file
-
Native Interfaces: Interact with Native Libraries. Native interfaces are used to interact with other programming languages.
-
Runtime Data Area: JVM memory
Execution process:
The source file (.java) is compiled by the compiler (javac.exe) into a bytecode file (.class). Bytecode files are loaded by the Class Loader of the classloading subsystem (the process of reading from hard disk to memory) into the Method Area of the runtime data Area. The bytecode file is a specification of the JVM’s instruction set and cannot be delivered directly to the underlying operating system. Therefore, a specific command parser Execution Engine is required to translate the bytecode into the underlying system instructions for Execution by the CPU. This process needs to call the local library interface of other languages to realize the function of the whole program.
A few words about the JVM runtime data area
The Java runtime data area includes:
-
VM Stack: The JVM allocates a Stack frame for each Java method executed. The stack frame holds the exit of the method, local variable table, parameters, operand stack, dynamic link, etc. A stack frame is the basic unit of a virtual machine stack.
-
Native Method Stack: provides the same functions as the VM Stack. However, the local method stack maintains native methods, which are written in C ++/ C.
-
Program Counter Register: An indicator of the line number of bytecode executed by the current thread. The bytecode parser’s job is to select the next bytecode instruction to execute by changing the value of this Counter. Branches, loops, jumps, and exception handling are all based on this counter
-
The Java Heap: The largest chunk of memory in the Java virtual machine, which is shared by all threads and where almost all objects and array instances are allocated memory.
-
Method Area: Used to store data such as class information, constants, static variables, and code compiled immediately that have been loaded by the VIRTUAL machine.
Details program counters
A program counter is a small memory space that can be viewed as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter works by changing the value of the program counter to select the next bytecode instruction to execute. Branches, loops, jump exception handling, and thread recovery depend on this counter.
In order to restore the thread to the correct execution position after switching, each thread has its own counter, which does not affect each other. This type of memory area is called “thread-private” memory
This program counter records the address of the virtual machine bytecode instruction being executed, but in the case of native methods, the program counter value is null (undefined).
This memory region is the only one that does not specify any OutOfMemoryError cases in the Java virtualization specification.
This section describes the Java virtual machine stack in detail
Like program counters, this space is thread-private and has the same lifetime as the thread.
The virtual machine stack describes the memory model of Java method execution: each method execution creates a stack frame that stores information about the method’s local variables, operand stack, dynamic links, method exits, and so on. ** Each method from call to end of execution corresponds to a stack frame in the virtual machine stack and out of the stack.
If the stack depth of the thread request is greater than the virtual allowed depth (when there are too many recursive methods called), StakOverflowError is raised.
An OutOfMemoryError is thrown if sufficient memory cannot be allocated during expansion.
What is a stack frame
Each method execution creates a stack frame that stores information about the method’s local variables table, operand stack, method exits, dynamic links, and so on.
What is a local variable scale? Operand stack?
Local variation scale: A local variable table stores local variables and method parameters stored within a method. This variable can be a primitive type (Boolean, byte, CHAR, short, int, float) or a reference type (returnAddress) Is the address of the bytecode instruction to be executed after return.
Operand stack: Like the local variable area, the operand stack is organized into an array of word lengths. Unlike the former, however, it is accessed not through indexes, but through standard stack operations-pushing and unpushing. For example, if an instruction pushes a value onto the operand stack, another instruction can pop that value up for use later.
Take a look at the local variable and operand stack with an example:
source
A local variable scale in an example method
public class T {
private int a = 0;
public void add(int b,int c){
a = b + c;
}
Copy the code
}
It turns out that the JVM secretly added a this reference to the local variable table when compiling the code (obviously this holds a reference to the instance), which is why we can access the instance’s member variables in the method, as follows
Operate the stack:
A brief explanation of the bytecode in the figure is as follows:
0) ALOad_0 pushes a reference to this onto the operand stack.
1) iloAD_1 pushes parameter B (the integer with index 1 in the local variable onto the operand stack)
2) ILoAD_2 pushes parameter C to the stack
The contents of the stack are (0 is the top of the stack)
0.c
1.b
2.this
3) Iadd adds the two numbers at the top of the stack and saves the result to the top of the stack, where the contents of the stack are
0.b+c
1.this
4). Putfield pushes the top two values off the stack. The first value (b+c) is assigned to the corresponding member variable of the second value (this).
Dynamic linking: All method references are stored in the constant pool of the method area as Symbolic references when the Java source file is compiled into a bytecode file. During the actual call, the symbolic reference is converted to a direct reference (the actual memory address of the reference), which is called dynamic linking.
For example: Person per = new Person() in Java source; The symbol reference is the full class name: com.johnny.person, and the memory address for the symbol reference is dynamically looked up when called.
Method exit: exit is return, if not normal, throw an exception.
Garbage collection algorithm
Is there a memory leak in Java?
There may be a memory leak in Java. Memory leaks are likely to occur when long-lived objects hold references to end-life objects.
Person p1 = new Person(” zs “,15);
public class Test01 {
static Vector v = new Vector(10); public static void main(String[] args) { for (int i = 0; i < 100; i++) { Object obj = new Object(); v.add(obj); obj = null; }}Copy the code
P1 is the reference variable, and new Person(” ZS “, 15) on the right is the object.
An object can have multiple references, and a reference can only be bound to zero or one object at a time.
What are Minor GC, Major GC, and Full GC?
MinorGC is the New generation of garbage collection. Because Java objects are mostly ephemeral, MinorGC is very frequent and generally fast to recycle. (Generally copy algorithm is used)
The Major GC is the old GC, which refers to the garbage collection action that occurred in the old era. Typically, Major GC is executed in conjunction with Minor GC. Major GC is much slower than Minor GC. (Mark-clear algorithm and mark-tidy algorithm can be used)
Full GC cleans the entire heap space, including Cenozoic and old generations.
Trigger conditions for Minor and Major GC
The Minor GC triggers the following conditions:
When Eden is full, the Minor GC is triggered.
Minor GC is triggered when the size of a newly created object is greater than the space Eden has left
The Major GC triggers the following conditions:
The size of objects promoted to the old age exceeds the old age free space
Heap memory allocates large objects
The trigger conditions for Full GC are as follows:
Perform Syste. Gc ()
CMS GC abnormal
Why should the New generation be divided into Eden and two Survivor regions?
- ** Reduces the number of Major Gc fires. ** If there is no Survivor, the surviving object will be sent to the old age every time a Minor GC is performed in Eden. The old age quickly fills up, triggering the Major GC. The memory of the old generation is much larger than that of the new generation, and a Major GC takes much longer than a MinorGC. Therefore, Eden and Survivor zones need to be divided.
- In addition, the point of Survivor is to reduce the number of objects sent to older generations, thus reducing the incidence of Major GC. Survivor pre-screening ensures that only objects that survive the new generation after 15 Minnor GCS are sent to the old age.
- Space fragmentation has been avoided. The newly created object is in Eden zone. After a Minnor GC, the surviving object in Eden zone will be copied to Survivor Space (namely S0) zone, and Eden zone will be emptied. When Eden is full, MinorGC will be triggered again. In this case, the surviving objects in Eden and S0 will be copied to the second Survivor Space (S1), and Eden and S0 will be emptied. This replication algorithm ensures that S1 living objects from S0 and Eden occupy contiguous memory space.
Why the generation (new generation and old generation)?
The main reason is that objects can be partitioned for storage according to the characteristics of each generation, making it easier to use the most appropriate garbage collection algorithm for recycling.
In the new generation: a large number of objects die and a few survive every garbage collection. Therefore, a collection can be completed with a small cost of living object replication. So the copy algorithm is used.
In the old days, because of the high survival rate of objects and the lack of additional space to guarantee, it was necessary to use “mark-clean” or “mark-tidy” algorithms.
Object storage process:
- Data is first allocated to the Eden region (large objects are placed directly into the old age). The JVM is triggered to issue a Minor GC when Eden runs out of space.
- If the object survives one Minor GC and is accepted by the Survivor space, it is moved to the Survivor space and the age is set to 1. The age of an object increases by 1 each time a Survivor survives a Minor GC. When they reach a certain age (15 by default), they are promoted to the old age.
Well, that’s all for today’s article, hoping to help those of you who are confused on the screen