JVM learning notes 02: garbage collection

garbage collection

How to judge whether garbage can be recycled

1: Reference counting method
When an object is referenced, the value of the referenced object is increased by one. When the value is 0, it means that the object is not referenced and can be recycled by the garbage collector.
This reference counting method sounds good, but it has one drawback, as shown in the figure below. During circular reference, the counts of both objects are 1, resulting in that neither object can be released.
2: Reachability analysis algorithm

  • The garbage collector in the JVM explores all living objects through reachability analysis
  • Scan the object in the heap to see if it can be found along the reference chain starting from the GC Root object. If it cannot be found, it indicates that it can be recycled
  • An object that can be used as a GC Root
    • Objects referenced in the virtual machine stack (local variable table in stack frame).
    • The object referenced by the class static attribute in the method area
    • Objects referenced by constants in the method area
    • The object referenced by JNI (generally called Native method) in the local method stack
public static void main(String[] args) throws IOException {

        ArrayList<Object> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add(1);
        System.out.println(1);
        System.in.read();

        list = null;
        System.out.println(2);
        System.in.read();
        System.out.println("end");
    }

reference type

  • Strong reference
    Only when all GC Roots objects do not reference this object through strong reference can this object be garbage collected
  • Soft reference
    When only the soft reference refers to the object, after garbage collection, if the memory is still insufficient, garbage collection will be started again to recycle the soft reference object
    The soft reference itself can be released in conjunction with the reference queue
  • Weak reference
    When only weak references refer to the object, the weakly referenced object will be recycled during garbage collection regardless of whether the memory is sufficient or not
    The weak reference itself can be released in conjunction with the reference queue
  • Phantom reference
    It must be used with reference queue, mainly with ByteBuffer. When the referenced object is recycled, the virtual reference will be queued,
    Virtual reference related methods are called by the Reference Handler thread to free direct memory
  • Finalizer reference
    No manual coding is required, but it is used internally with the reference queue. During garbage collection, the Finalizer references the queue (the referenced object has not been recycled for the time being), and then the Finalizer thread finds the referenced object through the Finalizer reference and calls its finalize method. The referenced object can be recycled at the second GC.

Show me weak references

/**
 * Demonstrate soft references
 * -Xmx20m -XX:+PrintGCDetails -verbose:gc
 */
public class Code_08_SoftReferenceTest {

    public static int _4MB = 4 * 1024 * 1024;

    public static void main(String[] args) throws IOException {
        method2();
    }

    // Set - Xmx20m, the demo heap is out of memory,
    public static void method1() throws IOException {
        ArrayList<byte[]> list = new ArrayList<>();

        for(int i = 0; i < 5; i++) {
            list.add(new byte[_4MB]);
        }
        System.in.read();
    }

    // Demonstrate soft references
    public static void method2() throws IOException {
        ArrayList<SoftReference<byte[]>> list = new ArrayList<>();
        for(int i = 0; i < 5; i++) {
            SoftReference<byte[]> ref = new SoftReference<>(new byte[_4MB]);
            System.out.println(ref.get());
            list.add(ref);
            System.out.println(list.size());
        }
        System.out.println("End of cycle:" + list.size());
        for(SoftReference<byte[]> ref : list) {
            System.out.println(ref.get());
        }
    }
}

method1 method parsing: first, set a heap memory size of 20m, and then run mehtod1 method. Exceptions will be thrown. The heap memory is insufficient because mehtod1
Lists in are strong references.

method2 method parsing: soft reference objects are stored in the list collection. When the memory is insufficient, full gc will be triggered to recycle the soft reference objects.

In the above code, when the object referenced by the soft reference is recycled, but the soft reference still exists, the general soft reference needs to be used with a reference queue.
Modify method2

// Demonstrates the soft reference collocation reference queue
    public static void method3() throws IOException {
        ArrayList<SoftReference<byte[]>> list = new ArrayList<>();
        // Reference queue
        ReferenceQueue<byte[]> queue = new ReferenceQueue<>();

        for(int i = 0; i < 5; i++) {
            // The reference queue is associated. When the byte [] associated with the soft reference is recycled, the soft reference will be added to the queue itself
            SoftReference<byte[]> ref = new SoftReference<>(new byte[_4MB], queue);
            System.out.println(ref.get());
            list.add(ref);
            System.out.println(list.size());
        }

        // Get the useless soft reference object from the queue and remove it
        Reference<? extends byte[]> poll = queue.poll();
        while(poll != null) {
            list.remove(poll);
            poll = queue.poll();
        }

        System.out.println("=====================");
        for(SoftReference<byte[]> ref : list) {
            System.out.println(ref.get());
        }
    }

Tags: Java jvm

Posted on Sun, 12 Sep 2021 13:37:31 -0400 by Ghostu