This is the fourth day of my participation in the August Text Challenge.More challenges in August
1 Reference counting method
Reference Count (Reference Count) adds a Reference counter to the object. Whenever it is referenced in a place, the value of the counter is +1. When the Reference is invalid, the value of the counter is -1.
When become 0 in this picture, this time using reference counting algorithm can determine it is junk, but reference counting method can’t solve a problem, is when the object is a circular reference counter value is 0, the reference counter cannot inform GC collector to recycle them, as shown in the figure below:
This is where we need to use our root-reachable algorithm.
2. Accessibility analysis
Root Searching means to search from the Root. When a program starts, those objects that are immediately needed are called Root objects. The so-called Root Searching algorithm is to find the Root object first, and then follow the line to find those useful ones. Common GC roots are as follows:
- Thread stack variables: Threads have a thread stack and a main stack frame. Objects that start inside main() are our root objects
- Static variable: A class has a static variable that is initialized as soon as it is loaded into memory, so the object that the static variable goes to is called the root object
- Constant pool: If your class uses objects from other classes, these are the root objects
- JNI: If we call classes or objects that are used by native methods written in C and C++
Figure of object5 and object6 although reference each other between them, but couldn’t find it from the root, so is rubbish, and object8 without any reference to natural is rubbish, other Object can be found from the root, so it is useful, will not be garbage collected.
GC Root
GC Roots is a set of must-active references. In layman’s terms, it is a potentially used object that a program can then access by direct or indirect reference. GC Roots include:
- In a Java thread, reference type parameters, local variables, temporary values, and so on for all methods currently being called. That is, the various references associated with our stack frame
- All currently loaded Java classes
- A Java class reference type static variable
- Reference type constants (String or Class) in the runtime constant pool
- The JVM internal data structures of some references, such as the sun. The JVM. Hotspot. The memory. The Universe
- Monitor objects for synchronization, such as objects whose wait() method is called
- JNI handles include global handles and Local handles
These GC Roots can be roughly divided into three categories:
- Various references to active threads
- Class reference to a static variable
- JNI references
There are two caveats:
- We are talking about active references here, not objects, which cannot be used as GC Roots
- The GC process is to find all live objects and consider the rest of the space “useless”; Instead of finding all the dead objects and reclaiming the space they take up. Therefore, even if the JVM heap is very large, the tracing GC can be very fast