“This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

🔴 introduction ☀ ️

Yesterday we talked about three problems with garbage collection: Reclaim which, when how to recovery and recycling, today let’s talk about the object inspection, we know the Wolf is the most dangerous in the world the most cunning animal, especially the wolves, tigers saw three points should be afraid of, when the wolves kill sheep nor hunt at any time, they are very patient, also understood the status of reconnaissance sheep, garbage collection is it not so? Before we start garbage collection, we need to know some garbage detection algorithms. Let’s learn about them.

🔴 Reference counting method ☀️

Our description of reference counting is as follows: We add a reference counter to an object, increments it by 1 when the object is referenced, and subtracts it by 1 when the reference is invalid. Any time the counter is 0, the object is unavailable (as if there is a ticking time bomb 💣).

Although ReferenceCounting is simple and efficient, it can be used in most cases. However, in the Java language, the dependencies between objects are complex and variable, so ReferenceCounting is not the best choice. For example, we will encounter circular dependencies in most cases. In circular dependencies, reference counters cannot be cleared and therefore cannot be reclaimed.

🔴 Reachability analysis algorithm ☀️

Because of the natural defects of reference counting, we have to look for other algorithms. Hence Reachability Analysis. Where does it start? Where do you go from “GC Roots”? Ha ha don’t care, as long as it can be on the chain of the root object, that is to say: from the root object, go all the way, no matter how to go, can reach the place to prove that the object is reachable, otherwise it is not reachable.

🔵 GCRoots 🔵

As mentioned earlier, “GC Roots” are the root objects. Since the root objects are objects, they must have types. What are the types? There are probably such a few, I give you a list (quick praise me 😄) :

  • Objects referenced in the virtual machine stack
  • A variable referenced by a static property in the method area
  • Object referenced by a constant in the method area
  • Local method stackJNIReferenced object
  • javaReference within the VM
  • All objects held by the synchronization lock
  • reflectJavaJMXBean,JVMTICallbacks, code caches, etc.

In addition to fixed root objects, are there any that are not fixed? The answer is of course! Users can according to the selected and recycling garbage collector area temporarily join some of the root object in, if only for a certain area of Java heap garbage collection, it needs to consider the virtual machine implementation details, targeted to join some associated region of the object to the GC Roots in the collection, to ensure the correctness of the accessibility analysis.

The 🔵 object references 🔵

Both reference counting and reachability analysis are inseparable from object reference. Before JDK1.2, the definition of object reference was still very general. Simply speaking, it was either a reference or no reference. For example, if you see a beautiful woman, it is a sex maniac, if you don’t see it, it is a gentleman.

Thus, after 1.2, Java further subdivides references into four types:

  • Strong reference (Strongly-Reference)
  • A weak reference (Weak-Reference)
  • Soft references (Soft-Reference)
  • Virtual reference (Phantom-Reference)

Strongly>Soft>Weak>Phantom

🔷 strong reference 🔷

Object obj = new Objet (); Object obj = new Objet (); In any case, as long as strong references exist, it is not a garbage collected object.

🔷 soft reference 🔷

Soft references are useful but not necessary objects that are garbage collected before the system runs out of memory and throw an exception if there is not enough memory.

🔷 weak reference 🔷

Weak references are even weaker than soft references, and when garbage collection occurs, the object associated with the weak reference will be cleared regardless of whether there is enough memory.

🔷 virtual reference 🔷

Virtual references are even weaker; the only purpose of setting a virtual reference is to be notified when the object is garbage collected.

💭 digression

How can Genghis Khan defeat a million armies of the Great Jin kingdom with only a few cavalry? Beat dozens of countries? Not only by the Wolf’s cruel, but also by the Wolf’s patience. No enemy is strong enough to be confused. Even a Wolf can kill a confused horse. Patience is not a Wolf, not a hunter, not Genghis Khan. If you want to figure out Wolf (garbage collection) or Genghis Khan (Java virtual machine), have time to learn it.