Methods of judging recoverable objects
1. Reference Counting algorithm
2. Accessibility Analysis Algorithm
Reference counting algorithm
Add a reference counter to the object, and whenever there is a reference to it, the counter value is incremented by 1,and when the reference fails, the counter value is reduced by 1; At any time the counter is 0 The object is impossible to be used again.
Flaw in reference counting algorithm: Circular reference
Accessibility analysis algorithm
The fundamentals of accessibility analysis algorithms:
The starting point is through some of the columns called "GC Roots", starting with these nodes and searching
The path traversed becomes the reference chain ( Reference Chain), when an object to the GC Roots No reference to the connection (in the case of graph theory, it is from the GC Roots When this object is unreachable), it is not available when this object is proved.
GC Roots in the Java language :
1. objects referenced in the virtual machine stack (local variable table in the stack frame)
2. object referenced in the class static property in the method area
3. The object referenced in a constant in the method area
4. object referenced by JNI ( that is, generally speaking, natve method ) in the local method stack
Garbage collection algorithms (both mark and clean up when virtual machines are paused)
1. tag - Clear Algorithm
2. Copying Algorithms
3. labeling - sorting algorithm
4. generation of collection algorithms
tag - purge algorithm
Basic principle:
algorithm is divided into Mark and clear Two stages: first mark all objects that need to be recycled, after the mark is complete
Unified collection of all tagged objects
The main flaw of the algorithm: a large number of discontinuous memory fragments are generated when the tag is cleared, and it is possible to advance in the case of creating large objects due to the lack of contiguous memory space Gc
Replication algorithm (for Cenozoic use)
Basic principle:
The available memory capacity is also divided into two blocks of equal size, using only one piece at a time. When this piece of
When the memory is exhausted, copy the surviving object to another piece, and then use the memory space
Clean it off at once.
Main flaw: Reduce memory to half the original
The new generation algorithm uses a copy algorithm, divided into a free space, two save space (8:1:1),Java the runtime can use a new generation of free space and a saving space, losing a saving space. The new belt recycles the available objects once to another save space, then GC for a free space and a save space .
Tag - collation algorithm: (used in the old age)
Basic principle:
The tagging process is still the same as the tag - purge algorithm, but the next step is not to clean up the heap-recyclable objects directly, but to have all the surviving objects move toward one end. Then directly clean out the memory outside the end boundary.
Major flaw: The marking and grooming phase must stop the execution of threads
Generational collection Algorithms
Rationale: Divides memory into blocks based on the different life cycles of the objects. The Java heap is generally divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm.
algorithm implementation of HotSpot virtual machine
1.GC Roots Enumeration
2. Safety points and safety zones
GC Roots Enumeration (thread stops execution when enumerating)
Difficulty: Check scope is large, must be in the snapshot, time enumeration
Accurate Type what the GC means: a virtual machine can know exactly what type of data is in a location in memory.
implementation of the HotSpot:oopmap Instance Demo
SafePoint meaning: There are many instructions that will cause oopmap content to change, and if the corresponding Oopmapis generated for each instruction ,it will require a lot of extra space, so The space cost of GC will become very high, so HotSpot is only in " specific location " This information is recorded, which is known as a security point (safepoint).
will produce SafePoint instruction Range: Make sequence reuse, such as method call, loop jump, exception jump, etc., so instructions with these functions will produce safepoint.
in the stop thread execution on SafePoint:
preemptive interrupt: Do not need the thread's execution code to cooperate actively, in When the GC occurs, all threads are first interrupted, and if the thread break is found to be out of the security point, the threads are resumed and run to the security point.
Active Interrupts: when a GC needs to interrupt a thread, it does not directly operate on the thread, simply sets a flag, and the individual threads actively polling for the flag when they execute, discovering that the interrupt flag is true when it interrupts its own suspension.
Saferegion : The first sign that you have entered Saferegion, so that during this time the JVM to launch a GC , you do not have to label yourself as a thread of Safe region state. when the thread is leaving the Safe region , it checks to see if the system has completed the root node enumeration (or the entire GC process), and if it does, it continues. Otherwise it must wait until it receives a signal that it can safely leave the safe region .
JVM Automatic Memory Management: Object determination and Recovery algorithm