1. Overview of garbage collection

Garbage Collection, in short, does three things:

  • What memory needs to be reclaimed?

  • When is it recycled?

  • How to recycle?

In the Java memory area:

  • The program counter, a virtual machine, local method stack three areas, according to the thread with a thread, the stack frame with the method of entry and exit and methodically performed on the stack and the stack operation, so this a few areas of memory recovery is certain, as the method of end or the end of the thread, memory natural recovery.

  • The Java heap and method areas have significant uncertainties: Multiple implementation classes of an interface may require different memory, and different conditional branches performed by a method may require different memory. Only during runtime can we know exactly which objects and how many objects will be created by the program. The allocation and reclamation of this part of memory is dynamic. The garbage collector is concerned with how this portion of memory is managed.

2. Determination of recycled objects

Now the first thing to do when getting into garbage collection: what memory needs to be reclaimed?

2.1 Reference counting algorithm

Let’s start with an older approach: reference counting.

The algorithm for reference counters looks like this: a reference counter is added to an object, incrementing its value every time it is referenced; When a reference is invalid, the counter value is reduced by one; An object whose counter is zero at any point in time cannot be used again.

This method is not currently the mainstream method of determination, because in addition to the extra space required to store counters and tedious update operations, reference counting has a major flaw in its inability to handle circular reference objects.

For example, suppose that objects A and B refer to each other, and there are no other references to either a or B. In this case, A and B are actually dead, but since neither of their reference counters is zero, they are still alive in the mind of reference counting. As a result, the space occupied by these circular reference objects is not recoverable, causing a memory leak.

2.2 Accessibility analysis algorithm

Currently, the mainstream Java virtual machine garbage collector adopts the reachability analysis algorithm. The essence of this algorithm is to take a series of GC Roots as the initial GC Root Set, and then start from the Set, explore all the objects that can be referenced by the Set and add them to the Set, which is also called mark. Ultimately, unexplored objects are dead and recyclable.

Fixed objects that can be used as GC Roots in Java technology architecture include the following:

  • The object referenced in the virtual machine stack (the local variable table in the stack frame)
  • An object referenced by a class static attribute in a method area, such as a Java class reference type static variable.
  • Objects that are constant references in the method area, such as references in the String Constant pool (String Table).
  • Objects referenced by JNI (commonly referred to as Native methods) in the Native method stack.
  • Internal references to the Java virtual machine, such as Class objects corresponding to basic data types, resident exception objects (NullPointExcepiton, OutOfMemoryError), and system Class loaders.
  • All objects held by the synchronized keyword.
  • Jmxbeans that reflect Java virtual machine internals, callbacks registered in JVMTI, local code caches, and so on.

References in Java

Whether the reference count algorithm is used to judge the number of references to the object, or whether the reference chain of the object is reachable by the reachability analysis algorithm, the survival of the object is dependent on “reference”.

There are four kinds of references in Java, which are Strongly Reference, Soft Reference, Weak Reference and Phantom Reference. The strength of these four kinds of references gradually decreases.

  • Strong references are the most traditional definition of “reference,” referring to reference assignments that are common in program code, and the garbage collector will never reclaim the referenced object as long as the strong reference relationship exists in any case.
Object obj =new Object();
Copy the code
  • Soft references are used to describe objects that are useful but not necessary. Only objects that are associated with soft references are listed in the recycle range for a second collection before an out-of-memory exception occurs. If there is not enough memory in the recycle range, an out-of-memory exception is thrown. After JDK 1.2, the SoftReference class was provided to implement soft references.
        Object obj = new Object();
        ReferenceQueue queue = new ReferenceQueue();
        SoftReference reference = new SoftReference(obj, queue);
        // The strong reference object is empty and the soft reference is reserved
        obj = null;
Copy the code
  • Weak references are also used to describe non-essential objects, but they are weaker than soft references, and objects associated with weak references only survive until the next garbage collection occurs. When the garbage collector starts working, objects associated only with weak references are reclaimed, regardless of whether there is currently enough memory. WeakReference classes were provided after JDK 1.2 to implement weak references.
        Object obj = new Object();
        ReferenceQueue queue = new ReferenceQueue();
        WeakReference reference = new WeakReference(obj, queue);
        // The strong reference object is empty and the soft reference is reserved
        obj = null;
Copy the code
  • Virtual references, also known as “ghost references” or “phantom references,” are the weakest type of reference relationship. The existence of a virtual reference does not affect the lifetime of an object, nor can an object instance be obtained through a virtual reference. The sole purpose of setting a virtual reference association for an object is to receive a system notification when the object is reclaimed by the collector. The PhantomReference class was provided after JDK 1.2 to implement virtual references.
        Object obj = new Object();
        ReferenceQueue queue = new ReferenceQueue();
        PhantomReference reference = new PhantomReference(obj, queue);
        // The strong reference object is empty and the soft reference is reserved
        obj = null;
Copy the code

4, unreachable! = death

Note that even if an object is judged unreachable, it does not have to die. For an inappropriate example, the object at this time is the death row, and the opportunity to redress.

If an object is found to have no reference chain connected to GC Roots after reachability analysis, it will be marked for the first time and then filtered by whether it is necessary to execute the Finalize () method. If an object succeeds in saving itself in Finalize (), it will be “saved” in the second tag if it reassociates with any object in the reference chain, such as assigning itself (this keyword) to a class variable or a member variable of the object. But if this opportunity is not seized, then the object really has to be reclaimed.



Reference:

[1] : Zhou Zhipeng, In Depth Understanding the Java Virtual Machine: Advanced JVM Features and Best Practices

[2] : Zhou Zhipeng et al translated Java Virtual Machine Specification

[3] : Feng Yafei, “Revealing the Design Principle and Implementation of Java Virtual Machine JVM”

[4] : Illustration of REFERENCE counting method and root search algorithm for JVM garbage collection

[5] : Java four kinds of references — strong, soft, weak, virtual knowledge summary