First, JVM basics
1. What is JVM
The JVM, short for Java Virtual Machine, is a specification for computing devices. The JVM is an imaginary computer that is implemented by emulating various computer functions on an actual computer. The Java virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field.
What is the relationship between JRE/JDK/JVM
JRE(JavaRuntimeEnvironment) is the Java platform. All Java programs must run under JRE. Common users only need to run the developed Java program and install the JRE.
The Java Development Kit (JDK) is a Development Kit used by program developers to compile and debug Java programs. JDK tools are also Java programs that require JRE to run. To maintain JDK independence and integrity, the JRE is also part of the JDK installation. Therefore, in the JDK installation directory, there is a directory named JRE to store the JRE file.
The JVM(JavaVirtualMachine) 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.
The JVM executes the process of the program
- Load the. Class file
- Manage and allocate memory
- Perform garbage collection
The JRE (Java runtime environment) is the environment in which Java programs constructed by the JVM run, but each application and process of an operating system has its own life cycle of running, as well as 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 hook it to a JNIENV(JNI calling interface) instance
- Call the JNIEnv instance to load and process the class class
4. JVM lifecycle
(1) The JVM instance corresponds to a standalone Java program running at the process level
- Start. When a Java program is started, a JVM instance is created, and any class that has the public static void main(String[]args) function can be used as the starting point for the JVM instance.
- 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 exit using the Runtime class or System.exit() if the security manager allows it.
(2) The JVM execution engine instance corresponds to the thread belonging to the user running the program, which is at the thread level
5. JVM garbage collection
Basic principle of Garbage Collection (GC) : Memory object is no longer used in recycling, for recycling in the GC method known as collector, because the GC need to consume some resource and time, Java in through analysis of the characteristics of the object’s lifecycle, according to the new and old s way to collect object, by as much as possible to shorten the GC pause for application.
- The collection of objects of the new generation is called the Minor GC
- The collection of objects from older generations is called the Full GC
- An active call to system.gc () in the program enforces a Full GC
Different types of object references are collected in different ways by GC. References to JVM objects fall into four types:
- Strong references: By default, objects are strongly referenced (instances of this object are not referenced by other objects until they are collected by GC)
- Soft references: Soft references are an application provided in Java that is suitable for caching scenarios (GC only when memory is low)
- Weak references: must be collected by the GC during GC
- Virtual references: Since virtual references are only used to tell if an object is GC
The process that a Java program executes:
- First Java source files (.java files) are compiled into bytecode files (.class files) by the Java compiler (javac.exe).
- The bytecode files of each class are then loaded by a ClassLoader in the JVM. After loading, the bytecode files are sent to the JVM execution engine for execution. The Runtime Data Area (Runtime Data Area) is used by the JVM to store Data and information needed during the execution of the program. Therefore, the memory management we often talk about in Java is the management of this space (how to allocate and reclaim memory space).
JVM memory structure /Java memory space classification
The JVM divides the memory it manages into several different data regions while a Java program is running: Program Counter Register, VM Stack, Native Method Stack, Method Area, Heap.
The method area is an area of memory in the virtual machine specification for run-time data areas. Different VIRTUAL machine vendors can have different implementations. The HotSpot VIRTUAL machine implements the method area in persistent generation, so the method area is a specification, and persistent generation is one of the implementations.
(I) program counter/register
Program Counter Register (Program Counter Register), also known as PC Register. A program counter is a small space that can be viewed as a line number indicator of the bytecode being executed by the current thread.
If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed. If the thread is executing a native method, this counter is undefined.
JVM multithreading is implemented by switching threads and allocating slices of CPU execution time so that only one CPU executes instructions in one thread at any one time. In order to ensure that the thread can be restored to the correct execution position after switching, each thread needs to have an independent program counter. The program counter between the threads is stored independently and does not affect each other.
This area is the only one where the Java Virtual Machine specification does not specify any OutOfMemoryError cases, because program counters are maintained internally by the virtual machine and do not require developer action.
(2) Java virtual machine stack
Describes the memory model of Java method execution: each method execution creates a “stack frame” to store information about local variables (including parameters), operation stack, method exit, and so on. Each method is called to the end of the execution process, corresponding to a stack frame in the virtual machine stack from the stack to the stack process. The declaration period is the same as the thread and is thread-private.
The local variable table stores basic data types known by the compiler (Boolean, byte, char, short, int, float, long, double), object references (referencing Pointers, not objects themselves), 64-bit longs and doubles take up space for two local variables, and only one for the rest. The memory space required for the local variable table is allocated at compile time. When entering a method, how many local variables the method needs to allocate in the stack frame is completely determined. Stack frames do not change the size space of the local variable table during runtime.
(3) local method stack
It is similar to the virtual machine stack except that the virtual machine stack serves Java methods executed by the virtual machine, while the Native method stack serves Native methods. The HotSpot VIRTUAL machine does not distinguish between the virtual machine stack and the local method stack, they are one and the same. Like the virtual stack, the local method stack throws StackOverflowError and OutOfMemoryError exceptions.
(4) the heap
The largest area of memory managed by the JVM, where object instances are held, is the thread shared area.
The heap is the primary area managed by the garbage collector and is therefore also referred to as the “GC heap.”
Class of JAVA heap:
-
In terms of memory reclamation, it can be classified into either the Eden space of the new generation (From or To Survivor space) or the old age (Tenured Gen).
-
From the perspective of memory allocation, in order to solve the problem of thread-safety when allocating memory, the JAVA heap shared by threads may be divided into multiple thread-private allocation buffers (TLabs).
The JAVA heap can be in a physically discrete memory space, as long as it is logically contiguous.
The size of the run-time heap memory can be specified with the -xmx-xms parameter. OutOfMemoryError is also thrown when the heap memory space is insufficient.
(5) Method area
Also known as “permanent generation” or “non-heap”, it is used to store information about classes loaded by the VIRTUAL machine, constants, static variables, and an area of memory shared by each thread. The default minimum value is 16MB and the maximum value is 64MB. You can limit the size of the method area by using the -xx :PermSize and -xx :MaxPermSize parameters.
Runtime constant pool: The part of the method area where the bulk of the content comes from the JVM loading classes. In addition to the Class version, field, method, interface, and other description information, the Class file contains the constant pool, which is used to store the various symbolic references generated by the compiler. This part of the content will be put into the runtime constant pool in the method area after the Class is loaded.
Direct Memory
Direct memory is not part of the virtual machine’s run-time data area, nor is it an area of memory defined in the Java Virtual Machine specification. It is allocated directly from the operating system and is therefore not limited by the Size of the Java heap, but is limited by the size of the total native memory and processor addressing space. Therefore, it may also cause OutOfMemoryError exceptions to occur. NIO, introduced in JDK1.4, is a new channel and buffer-based I/O approach that allocates direct memory directly from the operating system, that is, out of the heap, which can improve performance in some scenarios by avoiding copying data back and forth between the Java and Native heaps.
Third, summary
Program counters/registers: we have no control in the program
Heap: Holds data (object instances and arrays) generated with new
Stack: References to basic data types and objects, which themselves are not stored on the stack, but in the heap
Method area: Stores static members (global and static variables) and constants defined static in an object
Reference article:
Java memory space details
JAVA memory structure
Java memory space classification
JVM memory partitioning (JDK7 and JDK8)
Java Virtual machine memory composition and heap memory introduction