Java garbage collection mechanism

How to tell if an object is dead

  1. Reference counting method, only one place is referenced, the counter is incremented by 1, and the reference is invalid and decremented by 1
  2. Reachability analysis: Take the object of GC Roots as the starting point, search downward from these nodes, and the path traversed becomes a reference chain. When an object does not have any reference chain to GC Roots, the object is unreachable and can be recycled.

What are the GC Roots objects

  1. Objects referenced in the virtual machine stack
  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

Cited Category

  • Strong reference: A reference that has been used by new will not be recycled as long as the strong reference still exists.
  • Soft reference: Implemented through the SoftReference class to describe some useful but not necessary objects.
  • Weak reference: Non-essential objects are implemented through the WeakReference class. Objects that are weakly referenced will be recycled as long as GC occurs.
  • Virtual reference: It is implemented through the PhantomReference class. The instance of the object cannot be obtained through the virtual reference. The only function is to receive a system notification when the object is GC.

Garbage Collection Algorithm

mark-clear

  • Mark all objects that need to be recycled, and recycle all marked objects uniformly after the marking is completed.
  • shortcoming:
    • Both marking and clearing are inefficient.
    • There will be a lot of memory fragmentation after the mark is cleared.

replication algorithm

  • Divide the memory into two blocks of the same size and use only one of them at a time. When a block of memory is used up, the surviving objects are copied to another block, and the used memory space is cleared.
  • Most commercial virtual machines use this algorithm to recycle the young generation, but instead of using a 1:1 ratio, a larger Eden space and two smaller Survivor spaces are used each time Eden and one of the survivor spaces are used. When reclaiming, copy the surviving objects to another Survivor, and then clean up the two spaces. HotSpot uses 8:1:1.

Mark-Organize

  • First mark all objects that need to be recycled, move all surviving objects to one end, and then directly clean up the memory beyond the end boundary.
  • The old age uses this method.

Generational collection

  • The memory is divided into several blocks according to the life cycle of the object. Generally, the Java heap is divided into the new generation and the old generation, so that an appropriate collection algorithm can be used according to the characteristics of different generations.
  • In the new generation, a large number of objects will die each time a garbage collection is collected, and only a small number will survive. If the replication algorithm is used, only a small number of surviving objects need to be copied. Objects are more alive in the old age, so mark-sweeping or mark-sorting algorithms must be used.

Enter image description


Enter image description


Object Assignment Rules

  1. Objects are preferentially allocated in the Eden area, and a Minor GC is performed if the space is insufficient.
  2. Large objects are directly stored in the old age (large objects refer to objects that require contiguous memory space). This is done to avoid large memory copies between Eden and Survivor.
  3. Long-lived objects enter the old age. The virtual machine defines an age counter for each object. After one MinorGC, it will enter the Survivor area, and after each MinorGC, the age will be incremented by 1 until it reaches the threshold and the object enters the old age.
  4. Dynamically determine the age of the object. If the sum of the size of all objects of the same age in the Survivor area is greater than half of the Survivor space, objects whose age is greater than or equal to this age can directly enter the old age.
  5. Space allocation guarantee. Every time MinorGC is performed, the JVM will calculate the average size of objects moved from the Survivor area to the old area. If this value is greater than the remaining size of the old area, a FULLGC will be performed. If it is less than the value, check the HandlePromotionFailure setting. If true, only MinorGC will be performed. If true, then Do a FULL GC.

Guess you like

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