jvmGC (GarbageCollection) mechanism

First, what GC is the? Why should there be GC?

  • Java provides the GC function can automatically monitor the object exceeds the scope so as to achieve the purpose of automatic recovery of memory
  • Java language does not provide a method of operating the display release allocated memory.

Second, how to determine the object can be recycled garbage

1. Reference counting:

  • Reference counting is called to set a reference to each object counter, whenever there is a place to reference the object, the counter will be incremented by one, when referring to fail, a counter is decremented. When an object's reference count is zero, indicating that this object is not referenced, or "dead objects", it will be garbage collection.
  • Defects: can not resolve the circular reference problem , that is, when the object A references object B, the object B and referrer object A, then the time A, B object reference counter is not zero, thus causing garbage collection can not be completed, so mainstream virtual machine is not used in this algorithm.

2. reachability analysis

GC Roots can be used as objects in Java are the following:

  • VM stack referenced objects
  • Static metadata area property class object reference
  • Metadata constant pool area referenced objects
  • JNI native method stacks objects referenced

Thought: starting from the object of search is called GC Roots down, if an object is not connected to any reference GC Roots chain, then this object is not available.

Procedure: If the object in reachability analysis and GC Root is not a reference to the chain, then the time will be the first mark and conduct a screening , screening is whether the conditions necessary to perform a finalize () method. When the object does not cover the finalize () method has been called or virtual machine, then that is not necessary. If the object is necessary to perform a finalize () method , then the object will be placed in a called F-Queue to queue , the virtual opportunity to trigger a Finalize () thread to execute, this thread is a low priority, and virtual machine will not promise has been waiting for it to run to completion . ( This is because if the finalize () or slow implementation of a deadlock occurs, it will result in F-Queue queue have been waiting for, resulting in a collapse of the memory recovery system.) GC object in F-Queue is the second time mark, this time, the object will be removed "will recover" collection, wait for recovery.

Third, how to recycle garbage (collection algorithm)

Clear labeling algorithm (Mark-Sweep)

  • The most basic garbage collection algorithm is divided into two phases, marked and cleared. Mark phase mark all objects need to be recovered, the recovery phase is marked clear space occupied by an object
    The big problem is memory fragmentation algorithm severe, may occur subsequent large object can not find the problem of available space
  • The big problem is that the algorithm memory fragmentation severe , may occur subsequent large object can not find the problem of available space

Replication algorithm (Copying)

  • Press memory partitioning the memory into two equal size. Every time use only one copy to another block when the object of this one is full up after the memory is still alive, the memory has been cleared for use as:
    Here Insert Picture Description
  • Advantages: memory efficiency is not high, difficult to produce debris.
  • Disadvantages: the available memory is reduced to half of the original, if the increase in survival of the object, the algorithm efficiency will be greatly reduced

Tags to organize algorithm (Mark-Compact)

  • Mark phase and Mark-Sweep same algorithm, the tag is not clean up the object, but towards the end of the live objects in memory. Then clear the outer end of the object boundary
    Here Insert Picture Description

四、分代执行回收算法

新生代

  • 目前大部分JVM的GC 对于新生代都采取Copying算法,因为新生代中每次垃圾回收都要 回收大部分对象,即要复制的操作比较少。
  • 但通常按照8:1:1来划分新生代。一般将新生代划分为一块较大的Eden空间和两个较小的Survivor空间(From Space, To Space)
  • 当进行回收时,将该两块空间(eden和from space)中还存活的对象复制到另 一块To Space空间中。 如果To Space无法足够存储某个对象,则将这个对象存储到老生代。
  • 当对象在Survivor区躲过一次GC 后,其年龄就会+1。默认情况下年龄到达15 的对象会被 移到老生代中。
    Here Insert Picture Description

老年代

  • 而老年代因为每次只回收少量对象,因而采用Mark-Compact算法。

五、各种GC垃圾收集器

Here Insert Picture Description

  • Serial垃圾收集器
    • Serial(英文连续)是基本垃圾收集器,使用复制算法
    • 是java虚拟机运行在Client模式下默认的新生代垃圾收集器。
  • ParNew垃圾收集器
    • ParNew垃圾收集器其实是Serial收集器的多线程版本,也使用复制算法
    • 很多java 虚拟机运行在Server模式下新生代的默认垃圾收集器。
  • Parallel Scavenger收集器
    • 同样使用复制算法,也是一个多线程的垃圾收集器
    • 自适应调节策略也是 ParallelScavenge 收集器与 ParNew 收集器的一个 重要区别
  • serial Old收集器
    • 它同样是个单线程的收集器,使用标记-整理算法
  • Parallel Old收集器
    • 使用多线程的标记-整理算法,在JDK1.6 才开始提供。
    • 在年老代同样提供吞 吐量优先的垃圾收集器
  • Concurrent mark sweep(CMS)收集器
    • 它使用多线程的标记-清除算法。
    • 初始标记
    • 并发标记
    • 重新标记
    • 并发清除
  • G1收集器
    • 基于标记-整理算法,不产生内存碎片
    • 可以非常精确控制停顿时间,在不牺牲吞吐量前提下,实现低停顿垃圾回收

    六、永久代(元数据区)的垃圾回收

    • Garbage collection does not occur in the permanent generation is full or if permanent behalf of more than a threshold to trigger garbage collection (full gc)
Published 20 original articles · won praise 0 · Views 253

Guess you like

Origin blog.csdn.net/white_zzZ/article/details/104040653