Java Virtual Machine Study Notes (2)--Garbage Collection

How to determine which objects are garbage objects

  • reference counting

Implementation method:    Add a reference counter to the object. When the object is referenced somewhere, the value of the reference counter is +1. When the reference is invalid, the value of the counter is -1. When the value of the counter is 0, the value of the reference counter is +1. Represents this object as a garbage object, which can be recycled

Advantage:  high efficiency

Disadvantage:  Can't solve the problem of circular references between objects

Example:

/**
 * Determine which memory needs to be reclaimed, reference counting algorithm
 * -verbose:gc garbage collection log information
 * -xx:PrintGCDetail Print detailed GC information
 */
public class ReferenceCountDemo {
    private Object instance;

    public ReferenceCountDemo() {
        byte[] m = new byte[20 * 1024 * 1024];
    }

    public static void main(String[] args) {
        ReferenceCountDemo m1 = new ReferenceCountDemo();
        ReferenceCountDemo m2 = new ReferenceCountDemo();
        m1.instance = m2; //m1 object references m2
        m2.instance = m1; //m2 object references m1
        m1 = null; //clear the pointer of m1
        m2 = null; //clear the pointer of m2
        //After clearing the above pointers, since the m1 object and the m2 object refer to each other, the counters of the two objects are both 1, so they cannot be recycled

        System.gc();

    }
}

In the above example, m1 and m2 refer to each other, which makes it impossible to recycle, but we configure the jvm parameters, look at the garbage collection log, and find that the object has been recycled, because the garbage collector uses the reachability analysis method, so don't care. The execution result of the above example, as long as you understand what it means! The garbage collection log is as follows:

[GC (System.gc()) [PSYoungGen: 23830K->872K(38400K)] 44310K->21360K(125952K), 0.0049954 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 872K->0K(38400K)] [ParOldGen: 20488K->770K(87552K)] 21360K->770K(125952K), [Metaspace: 3303K->3303K(1056768K)], 0.0180230 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]

  • accessibility analysis

Implementation method:    Through a series of objects called "GC Roots" as the starting point, the search starts from these nodes, and the path traversed by the search is called the reference chain. When an object is not connected to the GC Roots by any reference chain, then Prove that this object is not available, so they will be determined to be recyclable objects.

Objects that can be used as GC Roots in the Java language include:

  1. Objects referenced by the virtual machine stack (local variable table)
  2. The object referenced by the static property of the class in the method area
  3. The object referenced by the constant in the method area
  4. Objects referenced by JNI in the native method stack

Example:

The two object pairs under Obj1 and below are reachable objects of GC Root, so they will not be reclaimed as garbage objects, while the two objects below Obj4 are not reachable objects of GC Root, so they will be reclaimed as garbage objects.


Garbage Collection Algorithm

  • mark-sweep algorithm

       The algorithm has two stages:       ①marking ②clearing

   

    shortcoming:

            1. Both processes of marking and clearing are inefficient

            2. After the mark is cleared, a large amount of memory fragmentation will be generated. Too much space fragmentation may cause that when memory is allocated to a large object, there is not enough contiguous memory allocated to the large object, which will trigger a garbage collection in advance. , it will consume time again!

  • replication algorithm

       In order to solve the efficiency problem of mark-sweep algorithm, the available memory is divided into two equal-sized blocks according to the capacity. Only one block is used at a time, and when this block is used up, the surviving objects are copied to another block. Then clear the used memory space once again.


Disadvantages:     space waste, general space will be wasted, when the object survival rate is high, more copy operations are required, so this algorithm is not suitable for the old age

Solution to space waste:

       The heap is subdivided into Survivor, Eden and Tenured Gen. When an object is created, the object is put into the Garden of Eden. After the memory of the Garden of Eden is divided, it will be placed in one of the Survivor areas. It is generally considered that the surviving objects are 10%. When garbage collection is performed, the surviving objects in one Survivor area and Eden will be copied to another Survivor area. When the Survivor area cannot be stored, it will be allocated to Tenured Gen.


The recycling process is as follows:


  • Mark Collation Algorithm

Move all surviving objects to one end, move all recyclable objects to the other end, and directly clean up memory outside the end boundary


  • Generational Collection Algorithm

The memory is divided into several blocks according to the different life cycles of the objects. Generally, the Java heap is divided into the new generation and the old generation. In this way, an appropriate collection algorithm can be adopted according to the characteristics of each age.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325857273&siteId=291194637