JVM Architecture
1. Class loading subsystem: Loading a class or interface with the appropriate name
2. Execution engine: Responsible for executing the instructions contained in the loaded class or interface.
Memory model
Each JVM will contain:
Method Area (persistent generation), Java heap (heap), Java stack (stack), instruction counter (register), local method stack.
Java heap: Saves an instance of an object.
Java stack: A pointer to the heap that holds the base type, returns the result, non-primitive type data.
Method Area: Holds class information, static variables, or constants that are defined as final.
Where the method area is shared with the heap for threads.
GC (Garbage Collection)
program counters, virtual machine stacks, and local method stacks are private memory spaces for each thread, which are born with threads and die with threads. For example, how much memory is allocated in each stack frame in the stack is basically known when the class structure goes down, so the memory allocation and recycling of these 3 regions is deterministic, regardless of the memory reclamation issue.
However, the method area and heap are different, one interface of the multiple implementation classes need the memory may not be the same, we only know when the program is running to create which objects, this part of the memory allocation and recycling are dynamic, the GC is mainly concerned about this part of memory
The method of judging an object that is dead: accessibility analysis.
All the generated objects are a subtree called "GC Roots", starting with the GC Roots and searching through the path to become the reference chain. When an object does not have an available reference chain to the GC roots, it is called an unreachable object.
Memory allocation
The automatic memory management advocated in the Java technology system can ultimately be attributed to automating the resolution of 2 problems: allocating memory to Objects and reclaiming memory allocated to Objects .
Object Precedence in Eden Assignment
In most cases, objects are allocated in the Cenozoic Eden region. When there is not enough memory in the Eden area, the virtual machine will initiate a minor GC.
- Minor GC (Cenozoic GC): Refers to the garbage collection action that occurs in the Cenozoic, because most Java objects have an out-of-date feature, so Minor GC occurs very often.
- Full Gc/major GC (old age GC): Refers to GC that occurs in the old age, Major GC appears, often accompanied at least once minor GC.
Large objects direct into the old age
Large objects are Java objects (such as long strings and arrays) that require a large amount of contiguous memory space.
Long-lived objects will enter the old age.
The JVM defines an object age counter for each object.
- If the object survives after Eden was born and has experienced the first minor GC and can be accommodated by survivor, it should be moved to survivor space, and the age object set to 1;
- Object in the Survivor area each time minor GC, age will increase by 1 years, when its age increased to a certain extent (by default, 15 years old, can be set by the parameter-xx:maxtenuringthreshold), will be promoted to the old age.
- It is important to note that the JVM does not always require that the age of the object must reach Maxtenuringthreshold in order to be promoted to the old age, if the sum of all objects of the same age in Survivor space is greater than the general size of survivor space, Objects older than or equal to that age can go straight into the old age, without having to wait for the ages required in the maxtenuringthreshold.
Space Allocation guarantee
- Before the minor GC occurs, the virtual opportunity first checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects, and if this condition is true, the minor GC is safe;
- If not, the virtual opportunity to see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, then hurry to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, although it is risky;
- If less than or handepromotionfailure is set to not allow adventure, then a full GC will be performed instead.
Java Virtual Machine (JVM) detailed