Analysis of the Java VIRTUAL Machine
Start with why learn the JVM
- A deeper understanding of Java, and an understanding of the JVM, leads to a deeper understanding of Java’s inner workings
- Resolve JVM issues such as memory overflow, GC frequency, etc
Understanding Java History
JavaSE, JavaEE, JavaME
-
Java SE (J2SE, Java 2 Platform Standard Edition, Standard Edition) Java SE was formerly known as J2SE. It allows the development and deployment of Java applications for use in desktop, server, embedded, and real-time environments. Java SE contains classes that support Java Web services development and provide the foundation for Java EE and Java ME.
-
Java EE (J2EE, Java 2 Platform Enterprise Edition, Enterprise Edition) Java EE was formerly known as J2EE. The Enterprise version helps develop and deploy portable, robust, scalable, and secure server-side Java applications. Java EE is built on Java SE and provides Web services, component models, management and communication apis that can be used to implement enterprise-level Service-oriented Architecture (SOA) and Web2.0 applications.
-
Java ME (J2ME, Java 2 Platform Micro Edition, Micro Edition) Java ME was formerly known as J2ME. Java ME provides a robust and flexible environment for applications running on mobile and embedded devices, such as cell phones, PDAs, TV set-top boxes, and printers. Java ME includes a flexible user interface, a robust security model, a number of built-in network protocols, and rich support for both networked and offline applications that can be downloaded dynamically.
The JDK and JRE
The Java Runtime Environment (JRE) contains only the required components for running Java programs, including the Java VIRTUAL machine and the Java core class library. The JDK (Java Development Kit) includes JRE as well as a series of Development and diagnostic tools.
If you only need to run Java programs, you can install the JRE. But if you’re developing Java programs, you’ll need to install the JDK.
The JVM architecture
- The subsystem
- Class loader
- Load the class file into the Runtime data area based on the fully qualified class name
- Execution Engine
- Execute bytecode instructions
- Methods are compiled into machine code and then executed using just-in-time compilation techniques
- Class loader
- component
- Runtime Data area Runtime data area
- The place where the runtime data is stored is the MEMORY of the JVM
- Native interface
- Interface to interact with Native Libraries
- Runtime Data area Runtime data area
process
The Java source code is converted into bytecode by the compiler. The class loader loads the bytecode into the runtime data area and the execution engine converts the bytecode into underlying system instructions for execution by the CPU
Java memory model
public
(Not thread-safe)
The Java heap
The primary working area of a Java program, created at JVM startup, that is used to allocate memory for instance objects (but also directly on the stack).
The Java NIO library allocates space like the direct memory area, which is faster to access than the Java heap and is considered for frequent reads and writes
Methods area
An area of Java class bytecode data that stores structural information for each class, such as runtime constant pools, field and method data, constructors, and so on. Store class-level data
private
PC register (program counter)
The Program Counter register, which holds the method that the thread is currently executing. The thread has no memory, and when a thread switch occurs, it needs to return to the thread state (i.e. where it has been executed), relying on the counter to do so
Java virtual machine stack
Java virtual machine stack, which is created at the same time as the thread and is used to store stack frames, where local variables and procedure results are stored. Stack frame stores data including: local variable table, operand stack.
Save: local variables, method parameters, Java method calls, returns
- The stack frame
- Local variable data
- The operand stack
- The frame data
Local method stack
The virtual machine stack serves the same purpose as the virtual machine stack, except that the virtual machine stack serves the Java methods, while the Native method stack serves the Native methods that the virtual machine calls
Java class loading mechanism
Start loading
- Launch classloader – responsible for loading classes from the launch classpath, nothing more than rt.jar. The loader is given the highest priority.
- Extension class loader – responsible for loading classes in ext directory **(jre\lib).**
- Application class loader – responsible for loading the application level classpath, including environment variables related to the path, etc.
Load bytecode into binary byte stream into memory
link
check
— Whether bytecode is correct
- JVM specification validation
- The JVM verifies the file format of the byte stream to see if it complies with JVM specifications and can be processed by the current version of the VIRTUAL machine.
- Code logic check
- The JVM verifies the data stream and control flow composed of the code to ensure that the JVM does not run the bytecode file with fatal errors.
To prepare
Allocate memory and initialize default values for static variables
- Memory allocation object
- The type of initialization
parsing
Replace the symbolic memory reference with the original reference
The JVM resolves 7 class references for class or interface, field, class method, interface method, method type, method handle, and call point qualifier. The main task of this stage is to replace its symbolic reference in the constant pool with its direct reference in memory.
Initialize the
- In the final stage, static variables are assigned and static blocks are executed
The garbage collection
Allocates allocated memory space
Determining garbage objects
Objects that are no longer referenced
Reference counting method
Set a count variable that counts +1 when referenced and -1 when referenced. When the count variable is 0, the object can no longer be referenced
The implementation is simple but does not solve the problem of circular references
GC Root Tracing
Reachability analysis algorithm: Search down from GC Roots, and the search path is called reference chain. When an object is not connected to GC Roots by any reference chain, the object is proven to be recyclable.
Memory partition
Recovery strategy
Generational recycling
Young Generation
Eden
Newly created class C1
From
After a minor GC, C1 survives to enter From
To
C1 survives after two minor GC’s with age+1
Again, minor GC
- Swap From and to if C1’s peer class does not reach half of the survivor space
- C1 enters the old age until it reaches more than half of survivor (the toSuv area is full)
Old Generation
The old chrono-space ratio exceeds the threshold starting marjor GC
algorithm
Replication algorithm
Copy algorithm. The core idea of the replication algorithm is to divide the original memory space into two pieces, one piece at a time, and copy the living objects in the used memory to the unused memory block during garbage collection. All objects in the memory block in use are then cleared, and the roles of the two memory blocks are swapped to complete garbage collection.
Mark clearing algorithm
During the marking phase, all reachable objects triggered by GC Root are marked. At this point, all unmarked objects are junk objects. Then, in the purge phase, all unmarked objects are cleared.
Mark compression algorithm
They are marked settlement and compression stage. During the marking phase, all objects are triggered to mark from the COLLECTION of GC Root references. In the compression phase, all living objects are compressed to one side of memory, and then all space outside the boundary is cleaned up.
algorithm | advantages | disadvantages | apply |
---|---|---|---|
Replication algorithm | Memory space allocation is continuous and efficient | The memory space is halved, reducing usage and requiring objects to be moved | Few viable objects |
Mark clearing algorithm | Simple implementation, do not move the object address | Space fragmentation, discontinuous memory space, reduced allocation efficiency | Multiple inventory items |
Mark compression algorithm | The marker clearing algorithm is optimized to reduce space debris |