This is the 12th day of my participation in Gwen Challenge

If an object is to be reclaimed, the most immediate consideration is whether the object is still usable. If the object is unusable, it is said to be “dead” and its memory can be reclaimed. In code, the use of an object is usually achieved by a reference to an object, so you can start by referring to an object to determine whether it is “dead”.

1 Reference counting algorithm

Reference counting algorithm: adds a reference counter to an object, incrementing the counter value by one each time a place references the object; When a reference is invalid, the counter value is reduced by one; When the reference count of an object reaches 0, the object is no longer usable and is “dead” and can be reclaimed.

The reference counting algorithm is simple, efficient, and can be said to be a very good algorithm, but Java does not use reference counting algorithm for memory management. The reason is that, in many cases, this seemingly simple algorithm requires a lot of extra processing to work correctly; For example, objects refer to each other in a circular manner.

public class Nodes{
    private String id = null;
    
    public Nodes next = null;
    
    public Nodes(String id){
        this.id = id;
    }
    
    public Nodes(String id, Nodes nodes){
        this.id = id;
        this.next = nodes;
    }
    
    public static void main(string[] args){
        Nodes n1 = new Nodes("001");
        Nodes n2 = new Nodes("002",n1);
        n1.next = n2;
        
        n1 = null;
        n2 = null; }}Copy the code

In the example above, since n1.next = n2; And n2 next = n1; , so n1 and n2 will not have zero reference counts. But since n1 = null; And n2 = null; Both objects will be unavailable. Therefore, a simple reference count will not complete the task of memory reclamation.

2. Accessibility analysis algorithm

Reachability analysis algorithms are used in the JVM to determine whether an object is alive or not. Reachability analysis algorithm: The root object of GC Roots is used as the starting node set, and the search process starts from GC Roots and searches down according to the reference relationship. The path through the search process is called reference chain. If there is no reference chain between an object and GC Roots, it means that the object is unreachable and cannot be used again.

As shown below, objects obj7 and obj8 refer to each other but are not reachable and will therefore be judged to be recyclable.

In the Java technology architecture, fixed objects that can be used as GC Root include the following;

  • Objects referenced in the virtual machine stack (the local variable table in the stack), such as parameters, local variables, temporary variables, etc. used in the method stack called by each thread;
  • The object referenced by the class static attribute in the method area;
  • An object referenced by a constant in a method area, such as a reference in a string constant pool;
  • Objects referenced in the local method stack;
  • Internal references to the Java virtual machine, such as resident exception objects, class loaders, and so on;
  • All synchronized locks (synchronizedKeyword) The object held;
  • Jmxbeans that reflect Java virtual machine internals, callbacks registered in JMVTI, native code caches, and so on.

3 Object Reference

No matter reference counting algorithm or reachability analysis algorithm, reference is indispensable to determine the survival of objects. In Java reference concept, there are four reference types:

  • Strong reference: is the most traditional definition of reference, referring to the common reference assignment in program code, i.eObject o = new Object()This reference relationship. In any case, the garbage collector will never reclaim the referenced object as long as the strong reference relationship exists.
  • Soft references: Used to describe objects that are still in use, but not required. Objects that are associated only with soft references are listed in the collection range for a second collection before the system is about to run out of memory. An out-of-memory exception is thrown if there is not enough memory for this collection. After JDK1.2, yesSoftReferenceClass to implement soft references.
  • A weak reference: describes objects that are not required, but are weaker than soft references. Objects associated with weak references only survive until the next garbage collection. When the garbage collector starts working, weak reference objects are reclaimed regardless of whether memory is sufficient. After JDK1.2, yesWeakReferenceClass to implement weak references.
  • Phantom reference: Also known as “ghost reference” or “phantom reference”, an object that has a virtual reference can no longer be used whether it is collected or not. A virtual reference is used only to receive a system notification when the object is collected by the garbage collector. After JDK1.2, yesPhantomReferenceClass to implement a virtual reference.

4 garbage recovery process

Garbage collection in Java follows a specific rule that objects are not collected if they can be uncollected, so even if an object is determined to be unreachable through reachability analysis, it is not immediately collected.

An object’s collection is marked twice.

An object is marked for the first time when it is judged unreachable. After being tagged for the first time, the JVM does a general filter to determine whether objects must execute the Finalize () method. If the object does not override the Finalize () method or finalize() method has already been called, then it is judged as “not necessary to execute”.

Objects that must be finalize() will be put into an F-queue, and then the VIRTUAL machine will automatically create a thread Finalizer to finalize() of objects in the F-queue. After the Finalize () method is executed, if the object is still unreachable, it will be marked a second time. Objects that are marked twice are finally reclaimed.