1. Composition of JVM#
- Class Loader: Loads .class files into JVM memory.
- Runtime Data Area: Mainly consists of Program Counter, Virtual Machine Stack, Native Method Stack, Method Area, and Heap.
- Execution Engine: Interprets JVM instructions, translates them into machine code, and submits them to the operating system after parsing is complete.
- Native Library Interface: A native library that integrates different programming languages and is available for Java to call.
- Native Method Library: The specific implementation of Java native methods.
2. Class Loader#
2.1 Classification
- Bootstrap Class Loader: Loads native method classes.
- Extension Class Loader: Loads extension classes implemented by the JDK.
- System Class Loader: Loads class files in the program.
2.2 Delegation Model
When loading a class file into JVM memory, the child class loader will dispatch the loading request to the parent class loader, and the parent class loader will load it first. If the parent class loader cannot load the class file, the child class loader will load it.
System Class Loader Extension Class Loader Bootstrap Class Loader
3. Execution Engine#
3.1 Translation#
- Just-in-time Compiler: For hot code in the program, the just-in-time compiler saves the corresponding machine code and uses the saved machine code to replace the same code encountered later.
- Bytecode Interpreter: Interprets bytecode into machine code.
4. Runtime Data Area#
4.1 Heap#
4.1.1 Components
- Young Generation: Includes Eden, Survivor (To, From) regions.
- Old Generation
4.1.2 Garbage Collection Mechanism
How to Determine Garbage
- Reference Counting: Assign a reference value to each object to indicate the number of times the object is referenced, which can cause circular reference problems.
- Reachability Analysis: Start from GC roots and traverse downwards. Objects that are not traversed are garbage.
GC roots include local variables referenced by stack frames in the virtual machine stack, local variables referenced by stack frames in the native method stack, static variables of classes in the method area, variables in the constant pool of the method area, and synchronized locking objects.
How to Collect Garbage
Mark and Sweep Algorithm, Mark and Copy Algorithm, Mark and Compact Algorithm, Generational Collection Algorithm
Classification
Young Generation Garbage Collectors: Serial, Parallel New, Parallel Scavenge
Old Generation Garbage Collectors: Serial Old, Parallel Old, CMS
Mixed: G1
When to Trigger Garbage Collection
- Young GC: Triggered when the Eden region in the young generation is full. Note that some surviving objects will be promoted to the old generation during young GC, so the occupancy of the old generation will usually increase after young GC.
- Major GC: Triggered when the old generation reaches a certain threshold (usually only performed by CMS?)
- Full GC:
- When creating a large object and the Eden region cannot accommodate the object, it will be directly stored in the old generation. If the old generation space is also insufficient, a Full GC will be triggered.
- When the Metaspace is full, a Full GC will be triggered.
- When allocating memory during young generation garbage collection, if the size of an object is larger than the available memory in the Survivor To space, the excess object will be moved to the old generation (this process is called allocation guarantee); if the available memory in the old generation is smaller than the size of the object, it will be judged whether the allocation guarantee fails; if allowed, a young GC will be attempted; otherwise, a Full GC will be triggered.
- Calling System.gc()
CMS
- Tri-color Marking Algorithm
- Main Process
- Initial Marking: STW, marks all GC roots directly associated with objects in the old generation (i.e., the GC roots themselves).
- Concurrent Marking: No STW, traverses objects marked as gray in the previous step, marks the traversed objects as gray, and marks the entry objects as black, repeating the above steps.
- Concurrent Precleaning: No STW, processes and updates dirty cards generated during concurrent marking.
- Remark: STW, mainly solves the problem of misjudgment. CMS sets a write barrier to mark objects that have modified internal references as gray, and then traverses the gray objects to prevent misjudgment.
- Concurrent Sweeping: No STW, clears all white objects.
- Disadvantages
- Occupies CPU resources.
- Generates floating garbage: During the concurrent cleaning phase, garbage is still being generated by the user, which needs to be cleaned up in the next GC.
- Memory fragmentation: CMS uses the mark-sweep algorithm, which generates a large amount of memory fragmentation.
- Concurrent failure: Due to the existence of floating garbage, CMS must reserve a certain amount of space to accommodate this newly generated garbage. CMS cannot wait until the old generation is full before cleaning up, as the Serial Old collector does. In JDK 5, CMS was activated when the old generation used 68% of the space and reserved 32% of the space to accommodate floating garbage. This is a relatively conservative configuration. If the old generation does not grow too fast in actual references, you can increase this value appropriately through the "-XX" parameter. In JDK 6, the triggering threshold was raised to 92%, reserving only 8% of the space to accommodate floating garbage. If the reserved memory of CMS cannot accommodate the floating garbage, it will cause "concurrent failure", and the JVM has to trigger the contingency plan to enable the Serial Old collector to collect the old generation, which will result in a longer pause time.
G1
- Partitioning
G1 adopts the idea of partitioning (Region) and divides the entire heap space into several equally sized memory regions. Each time an object space is allocated, it will be used in segments. Therefore, G1 does not require the storage of objects to be physically contiguous in the heap, as long as they are logically contiguous; each partition is not necessarily dedicated to serving a certain generation, and can be switched between the young generation and the old generation as needed. The partition size can be specified at startup using the parameter -XX=n (1MB~32MB, and must be a power of 2), and the entire heap is divided into 2048 partitions by default.
In G1, there is also a special region called the Humongous region. If an object occupies more than 50% of the partition capacity, the G1 collector considers it a humongous object. These humongous objects are usually allocated directly in the old generation, but if it is a short-lived humongous object, it will have a negative impact on the garbage collector. To solve this problem, G1 divides a Humongous region specifically for storing humongous objects. If an H region cannot accommodate a humongous object, G1 will search for consecutive H regions to store it. In order to find consecutive H regions, sometimes it is necessary to start a Full GC. - Object Allocation Strategy
- TLAB (Thread Local Allocation Buffer): TLAB is used for thread-local allocation buffer, which aims to allocate objects as quickly as possible. If objects are allocated in a shared space, some synchronization mechanisms are needed to manage the free space pointers in these spaces. In the Eden space, each thread has a fixed partition for allocating objects, namely a TLAB. When allocating objects, there is no need for synchronization between threads.
- Allocation in Eden Space: For objects that cannot be allocated in the TLAB space, the JVM will try to allocate them in the Eden space. If the Eden space cannot accommodate the object, it can only be allocated in the old generation.
- Humongous Region Allocation
-
Young GC Process
- Initial Marking: STW, marks all GC roots directly associated with objects (i.e., GC roots themselves).
- RSet Processing and Updating: Processes the information of the RSet and scans it to add references from old generation objects to young generation objects under GC Roots.
- Cleaning Phase: STW, packs all young regions into CSet. If the predicted collection time is less than the given tolerance time, G1 will increase the number of regions in the Eden area without cleaning; otherwise, if the predicted collection time is greater than the given tolerance time, G1 will perform mark-copy operation and copy all surviving objects to empty survivor regions.
-
Mixed GC Process
-
Initial Marking: This process is performed together with the pause process of young GC.
-
Root Region Scanning
-
Concurrent Marking Phase: If there is a reference modification (excluding memory allocation for objects), the write barrier captures the original value of these references and records them in the log buffer. The subsequent marks are based on the original values, not the new values. For newly created objects, G1 uses a different method. G1 uses two TAMS variables to determine newly created objects. One is called previous TAMS, and the other is called next TAMS. Objects between the two are newly allocated objects.
-
Remark: STW, processes the SATB write barrier objects left by each thread.
-
Cleaning Phase