JVM learning -- garbage collection algorithm

What is rubbish

Why GC


java garbage collection mechanism




Garbage collection related algorithms

Marking phase

Reference counting algorithm

  • The reference counting algorithm saves an integer reference counter attribute for each object to record the reference of the object.
  • For an object A, as long as another object references A, the reference counter of A is + 1. When the reference fails, the reference counter is - 1. As long as the value of the reference counter of A is 0, it means that A is not used and can be recycled
  • Advantages: simple implementation, easy identification of garbage objects, high judgment efficiency, and no delay in recycling,
  • Disadvantages:
    -Separate fields are required to store counters, which increases the overhead of storage space
    -Each assignment needs to update the counter, which increases the time overhead with the addition and subtraction operation
    -The problem of circular reference cannot be solved, which leads to the fact that this kind of algorithm is not used in java garbage collector

Reachability analysis algorithm (root search algorithm 𞓜 traceable garbage collection)





Object finalization mechanism




public class CanReliveObj {
    public static CanReliveObj obj;//Class variable, belonging to GC Root


    //This method can only be called once
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("Call the current class override finalize()method");
        obj = this;//The object to be recycled at present is connected with an object obj on the reference chain in the finalize() method
    }


    public static void main(String[] args) {
        try {
            obj = new CanReliveObj();
            // The object successfully saved himself for the first time
            obj = null;
            System.gc();//Call garbage collector
            System.out.println("1st time gc");
            // Because the Finalizer thread has a low priority, pause for 2 seconds to wait for it
            Thread.sleep(2000);
            if (obj == null) {
                System.out.println("obj is dead");
            } else {
                System.out.println("obj is still alive");
            }
            System.out.println("Second time gc");
            // The following code is exactly the same as the above, but this self rescue failed
            obj = null;
            System.gc();
            // Because the Finalizer thread has a low priority, pause for 2 seconds to wait for it
            Thread.sleep(2000);
            if (obj == null) {
                System.out.println("obj is dead");
            } else {
                System.out.println("obj is still alive");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Clear phase

Mark sweep algorithm



Mark copy algorithm




Tag compression algorithm 𞓜 tag sorting algorithm (Mark compact)





Summary

Generational collection algorithm



Incremental collection algorithm


Partition algorithm

Tags: Java jvm

Posted by underparnv on Sat, 21 May 2022 03:49:12 +0300