The concept of GC
Garbage Collection Garbage Collection
1960, the list language uses the GC
In Java, GC objects are heap space and permanent zone
GC Algorithm Reference Counting method
Veteran garbage Collection algorithm
Recycle Garbage by reference calculation
The implementation of the reference counter is simple, for an object A, as long as any one object refers to a, then the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. Object A can no longer be used as long as the value of the reference counter for object A is 0.
Issues with reference counters
1. Poor performance
2. Unable to process circular references
Markup Cleanup algorithm
The mark-and-sweep algorithm is the basic idea of modern garbage collection algorithm. The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase. A feasible implementation is to mark all the objects that can be reached from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared.
Tag compression algorithm
Used in situations where there are many surviving objects, such as the old age
It does some optimizations based on the mark-and-sweep algorithm. Like the mark-and-sweep algorithm, the tag-compression algorithm first needs to start with the root node and mark all objects that can be reached. However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory. After that, clean up all the space outside the boundary.
Copy algorithm (requires a separate space to guarantee)
Compared with the tag-purge algorithm, the replication algorithm is a relatively efficient recovery method
Not suitable for situations where there are many surviving objects, such as the old age
Divide the original memory space into two pieces, one at a time, and at garbage collection, copy the surviving objects in the memory being used into the unused memory block, clear all objects in the memory block in use, swap the two memory roles, complete the garbage collection
Problem:
Wasted Space
Generational thinking
According to the survival period of the object, the short-lived object is classified as the new generation, and the long-life object belongs to the old age
According to the characteristics of different generations, choose the appropriate collection algorithm
Short-lived objects, suitable for replication algorithms
Longevity object for tag cleanup and tag compression
Accessibility
1. Objects that can be touched from the root node
2. The Resurrection
Cannot be touched
1. Untouchable objects cannot be resurrected.
2, can be recycled
3, the Finalize () method may enter a non-reachable state
What is root:
1. Reference objects in the stack
2. Objects that are referenced by static members and constants in the method area
3, the Reference object in the Jin method stack
Stop the word
A global pause phenomenon in Java
Global pause, all Java code stops, Nativa code can execute, but cannot interact with JVM
Mostly due to GC
Damage: Long service stop, no response
Encountering an HA system, which can cause primary and standby switching, seriously compromising the production environment
Algorithms and types of GC