JVM memory model – How does memory allocation relate to the JDK, JRE, and JVM
1. What is JVM? [Obtaining resources]
- []
jvm
A specification for computing devices, a fictional machine that simulates functions on an actual computer. -
jvm
Contains a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field. -
JVM
Masking information specific to a particular operating system platform allows Java programs to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java virtual machine.JVM
When bytecodes are executed, they are actually interpreted as platform-specific machine instructions.
What is the relationship between JDK, JRE, and JVM? [Obtaining resources]
- JRE(
Java Runtime Environment
), the Java platform. All Java programs must run in the JRE environment. - JDK(
Java DevelopmentKit
), is the developer used to compile, debugging program development package. The JDK is also a JAVA program that needs to run on the JRE. - JVM(
Java VirtualMachine
) is part of the JRE. It is an imaginary computer by simulating various computer functions on an actual computer.
The JVM has its own complete hardware architecture, such as processors, stacks, registers, and the corresponding instruction system. The most important feature of the Java language is that it runs across platforms. The PURPOSE of using the JVM is to enable cross-platform implementation independent of the operating system.
3. Principle of JVM
- The JVM is the core and foundation of Java, the virtual processor between the Java compiler and the OS platform on which bytecode programs can be executed.
- As long as the Java compiler is oriented to the JVM, it generates bytecode files that the JVM can understand. Java source files are encoded into bytecode programs that translate each instruction into a different machine code through the JVM and run on a specific platform. (JIT just-in-time compiler
Just-In-Time Compiler
)
- The process by which the JVM executes a program
- Loading. Class filesJava class loading mechanism
- Manage and allocate memory
- Performing garbage collection
JRE (Java runtime Environment) The runtime loop of a Java program constructed by the JVM. It is also the environment in which a Java program is run, but it also has an application and a process of an operating system, so it has its own runtime life cycle and its own code and data space. The JVM is the lowest level of the JDK, responsible for operating system interaction, and is used to mask the operating system environment and provide a complete Java runtime environment, hence the virtual computer.
Loading the OPERATING system into the JVM is done by using Java. Exe in the JDK. The following 4 steps are used to complete the JVM environment:
- Create the JVM load environment and configuration
- To load the JVM. DLL
- Initialize jVM.dll and mount to
JNIENV
(JNI call interface) instance - Call the JNIEnv instance to load and process the class class.
- Lifecycle of the JVM
(1) The JVM instance corresponds to a standalone Java program running at the process level
- Start. When a Java program is started, an instance of the JVM is created, and any JVM that has public static void main(
String[]args
The class of the) function can be used as a starting point for JVM instances to run. - Run. Main () is the starting point for the program’s initial thread, which starts any other threads. There are two kinds of threads inside the JVM: daemons and non-daemons. Main () is a non-daemon thread, and daemons are usually used by the JVM itself. Java programs can also indicate that they are creating daemons.
- Die. The JVM exits when all non-daemons in the program terminate; The program can also be used if the security manager allows it
Runtime
Class orSystem.exit()
To quit
(2) The JVM execution engine instance corresponds to the thread belonging to the user running the program, which is at the thread level.
6. JVM memory model
(1) The specific execution process of Java code is shown in the following figure.(2) The runtime data area, namely THE JVM memory structure, is shown below[Obtaining resources]
(3) What data is stored in the runtime data area? [Obtaining resources]
- Program counter (PC register)
Since multithreading in the JVM is achieved by switching between threads to obtain CPU execution time, the kernel of a CPU will execute instructions in only one thread at any given time. Therefore, in order to ensure that each thread can return to its original execution position after the switch, Each thread needs to have its own independent program counter, and can not interfere with each other, otherwise it will affect the normal execution order of the program. Therefore, it is safe to say that program counters are private to each thread. Since the amount of space stored in the program counter does not change with the execution of the program, there is no OutOfMemory for the program counter.
- Java stack The Java stack is a stack frame, each stack frame corresponds to a called method, in the stack frame includes local variable table
(LocalVariables)
Operand stack(Operand Stack)
, a reference to the runtime constant pool of the class to which the current method belongs (the concept of runtime constant pool is covered in the method area section)(Reference to runtime constant pool)
Method return address(Return Address)
And some additional information. When a thread executes a method, it creates a corresponding stack frame and pushes the stack frame. When the method completes execution, the stack frame is pushed off the stack. - Local method stack The local method stack works and works much like the Java stack. The difference is simply that the Java stack serves to execute Java methods, whereas the local method stack serves to execute local methods
(NativeMethod)
The service. - Heaps in Java are used to store objects themselves and arrays (array references are stored in the Java stack). The heap is shared by all threads in
JVM
There is only one heap in. - The method area, like the heap, is an area shared by threads. In the method area, the information of each class (including the name of the class, method information, field information), static variables, constants, and the compiled code of the compiler are stored. In addition to the description of a Class’s fields, methods, interfaces, and so on, another piece of information in the Class file is the constant pool, which stores literal and symbolic references generated during compilation. A very important part of the method area is the runtime constant pool, which is a runtime representation of the constant pool for each class or interface that is created after the class or interface is loaded into the JVM. Of course, only the contents of the Class file constant pool can be entered into the runtime constant pool. New constants can also be added to the runtime constant pool at run time, such as the String intern method.
7. JVM memory overflow
(a) Program Counter Register Each thread has an independent Program Counter, and the counters of each thread do not affect each other, so this area is private to the thread. This memory area is the only one in the Java Virtual Machine specification that does not specify any OOM (memory overflow:OutOfMemoryError
) area of the situation.
(b)Java Virtual Machine Stacks
In the Java Virtual Machine specification, two exceptions are specified for this area:
- If the thread requests a stack depth greater than the virtual machine allows, the stack is thrown
StackOverflowError
The exception. - 2. If the VM cannot obtain sufficient memory space while dynamically expanding the stack, this event is thrown
OutOfMemoryError
The exception. There is some overlap between the two cases: when stack space can no longer be allocated, either too little memory or too much used stack space is essentially just two ways of describing the same thing.
In single-threaded operations, the virtual machine throws StackOverflowError and does not get OutOfMemoryError when stack space cannot be allocated, either because the stack frame is too large or because the virtual machine stack space is too small. [Obtaining resources]
In a multithreaded environment, OutOfMemoryError is thrown.
The Java Heap is the largest chunk of memory managed by the Java VIRTUAL machine, which is an area of memory shared by all threads. Almost all object instances and arrays are allocated memory here. The Java Heap is the primary area managed by the garbage collector, and is often referred to as the “GC Heap.”
According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space as long as it is logically contiguous. OutOfMemoryError is thrown if there is no memory to allocate in the heap and the heap cannot be expanded.
(d) The method area, also known as the “persistent generation,” throws an OutOfMemoryError when the method area cannot meet memory allocation requirements.
About garbage collection algorithm (1) Currently, CMS algorithm (Concurrent Mark-sweep) algorithm is extended based on mark-sweep algorithm
(2) G1(Garbage First) algorithm
Click “Get Resources”, free full set of Java resources. Add micro letter also can 15608464306 finally wish everyone academic achievement oh !!!! Please like and follow !!!!