GC and memory allocation strategy

A, GC

  The first step: to determine whether the object is dead? There are two methods: the first one is a reference counting method , i.e., adding a reference to an object counter, when referred to, the counter + 1; when referring to failure to 1; when the counter is 0 indicating that the object is not references. But the disadvantage is that the counter: lead to the mutual references between objects counter is not zero and can not be recovered. The second method is reachability analysis , i.e., by defining a set of GC Roots object as a starting point, the starting point downward from the search, when an object has no references to GC Roots chain, then this object is not available .

    As GC Roots of objects including: an object referenced in a virtual machine stack, static approach to the object class property or constant references, native method stacks objects in native method references.

    Here again explain the quote! ! !

    References are divided into : a strong reference: a reference to the object is assigned to a variable, the variable is referenced strong reference. (Object strong references will not be recovered)

         Soft Quote: There used to describe some, but not necessarily object. (Before the system is going to happen overflow exception, these objects will be included in the scope of recovery for secondary recovery, if there is not enough memory after recycling, will throw an exception. SoftReference class implements the use of soft references )

         Weak references: non-essential to describe the object, its intensity weaker soft reference. (When GC occurs, regardless of the current memory is sufficient, the recovery will be a weak object reference associated. WeakReference class to implement using weak references )

         Phantom reference: The main role is to track the object is garbage collected state, it is the weakest of reference relationships. (The PhantomReference class to implement virtual reference)

  Death of an object, at least to go through the process twice marker: If an object during reachability analysis found no references to GC Roots connected to the chain, then it will be the first mark and conduct a screening, screening conditions this object is whether it is necessary to perform the finalize () method. If the object does not cover the finalize () method or finalize method has been called a virtual machine, it is not necessary to perform. If the object is determined to be necessary to finalize the implementation of the method, the object will be placed in a known F-Queue queue, and the establishment of a virtual machine at a later automatically by a low priority Finalizer thread to execute it.

  finalize method is the last chance to escape death fate of the object, later GC will be F-Queue objects in the second mark, if the object associated with any re-establish a chain of object references, it will mark the second time it will remove the collection "will recover"; and if the object did not escape, then it really was recovered.

  Area recovery method: garbage collection with the main permanent constant recycling of waste and useless classes. Determining a class is not "useless class" Condition: All instances of the class have been recovered; ClassLoader loaded class has been recovered; java.lang.Class class corresponding to the object is not referenced in any place, not by such reflective access method.

  Step two: garbage collection algorithm

    The first is to mark - sweep algorithm (Mark-Sweep) , mark and sweep algorithm is divided into two parts: first mark all objects to be recovered, after completion of all marking objects tagged uniform recycling. Cons: a mark and sweep the two processes is not efficient, and the other is a large amount of space debris after the discontinuity mark and clear, leading to not find enough memory after allocating large objects prematurely trigger GC.

    The second is the copy algorithm (COPYING) , by which the available memory capacity is divided into two equal size, which uses only one. When this piece of memory is used up, it will also copy objects to survive on top of another piece, then put the memory has been used once cleared away. Pros: Fixed a memory fragmentation, simple, efficient operation; drawback: the memory is reduced to half of the original, so the larger the cost.

    The third is labeled - Finishing Algorithm (Compact-Mark) , is divided into two portions labeled and finishing, First all objects to be recovered, all live objects are moved to an end, and then clean out the direct memory outside end edges .

    The fourth is a generational collection algorithm , the memory is divided according to the different objects in the inventory cycle and the old for the new generation's, and then use the appropriate algorithm according to the characteristics of each decade. Each has a large number of objects in the new generation died, only a few survive, then using the copy algorithm; and the object's old high survival rate with no extra space for it to be secured, it is necessary to use the mark - sweep or mark - sorting algorithm to recover .

  The third step: the garbage collector

    Serial collector , the collector is a single-threaded, must suspend all other thread work it carried out GC, until it is collected by the end of this work is actually a virtual machine to automatically initiate and automatically in the background.

    ParNew collector , is a multi-threaded version of the Serial collectors, in addition to the use of multi-threaded garbage collection, the other acts as a collector and Serial.

    Parallel Scavenge收集器,是使用复制算法的新生代收集器,又是并行的多线程收集器。它的目的是达到一个可控制的吞吐量,高效率地利用CPU时间, PS收集器提供了控制最大垃圾收集停顿时间的 -XX:MaxGCPauseMillis参数和直接设置吞吐量大小的 -XX:GCTimeRatio参数用于精确控制吞吐量。(PS收集器还有一个参数-XX:+UseAdaptiveSizePolicy,这是一个开关参数,这个开关打开后,就不用手工指定新生代的大小、Eden和Survivor区的比例、晋升老年代对象的大小等细节参数,虚拟机会根据当前系统的运行情况收集性能监控信息动态调整这些参数,这种调节方式称为GC自适应的调节机制)。

    Serial Old收集器,是单线程收集器,使用标记-整理算法,是Serial收集器的老年代版本,是给Client模式下的虚拟机使用。

    Paralled Old收集器,是Parallel Scavenge收集器的老年代版本,使用多线程和标记-整理算法。

    CMS收集器,是一种以获取最短回收停顿时间为目标的收集器,使用多线程和标记-清楚算法。

      运作过程分为:初始标记(仅仅只是标记一下GC Roots能直接关联到的对象)

             并发标记(进行GC Roots Tracing 的过程)

             重新标记(为了修正并发标记期间因用户程序继续运作而导致标记产生变动的那一部分标记记录)

             并发清除(清除GC Roots不可达的对象,不需要暂停工作线程)。

      优点:并发收集、低停顿;

      缺点:CMS收集器对CPU资源非常敏感、CMS收集器无法处理浮动垃圾、基于标记-清除算法会产生大量的空间碎片。

    G1收集器,是一款面向服务端的垃圾收集器,G1具备并行与并发、分代收集、空间整合(基于标记-整理和复制算法实现,不会产生空间碎片)、可预测停顿的特点;G1收集器中它将Java堆划分为多个大小相等的独立区域,独立使用和回收,这样可以控制一次回收多个区域,减少一次GC所产生的停顿(区域划分机制)。G1之所以能建立可预测的停顿模型,是因为G1跟踪各个区域里面的垃圾堆积的价值大小,在后台维护一个优先列表,每次根据允许的收集时间,优先回收价值最大的区域(优先区域回收机制)。为了避免全堆扫描,G1使用Remembered Set来管理相关的对象引用信息。当进行内存回收时,在GC根节点的枚举范围中加入Remembered Set即可保证不对全堆扫描也不会有遗漏。

      运作过程分为:初始标记(仅仅只是标记一下GC Roots能直接关联的对象,并修改TAMS的值,让下一阶段用户程序并发运行时,能在正确可用的region中创建对象,此阶段需要停顿线程,但时间很短)

             并发标记(从GC Root开始对堆中的对象进行可达性分析,找出存活的对象,耗时长,但可与用户线程并发执行)

             最终标记(为了修正并发标记期间因用户程序继续运行而导致标记产生变动的那一部分标记记录并把Remembered Set Logs的数据合并到Remembered Set 中,此阶段需要停顿线程,但可并发执行)

             筛选回收(首先根据各个region的回收价值和成本进行排序,根据用户所期望的GC停顿时间来制定回收计划)

    并行(Parallel):指多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态。

    并发(Concurrent):指用户线程与垃圾收集线程同时执行。

二、内存分配与回收策略

  大多数情况下,对象分配在新生代的Eden区上,分配规则是不固定的,取决于当前使用的垃圾收集器以及虚拟机中与内存相关的参数设置。

  新生代:用来存放新产生的对象,占用1/3空间;新生代分为一个Eden区和两个Survivor区(FromSurvivor和ToSurvivor);当Eden区没有足够空间进行分配时,虚拟机将会进行一次Minor GC。进行一次Minor GC后的对象进入FromSurvivor区,当From和Eden区没有可用空间时,会进行第二次Minor GC,将存活的对象移到ToSurvivor区。

  大对象(需要大量连续内存空间的对象)直接进入老年代;虚拟机提供了一个-XX:PretenureSizeThreshold参数,令大于这个设置值的对象直接在老年代分配,这样做避免了在Eden区及两个Survivor区之间发生大量的内存复制。长期存活的对象将进入老年代,如果对象在Eden区出生并且经过第一次Minor GC后仍然存活,将被移动到Survivor区,并且对象年龄设为1。对象在Survivor区中每经过一次Minor GC,年龄就+1,当它的年龄增加到一定程度时(默认为15),就将会被晋升到老年代中。对象晋升老年代的年龄阈值,可以通过参数-XX:MaxTenuringThreshold设置。

  动态对象年龄判定:如果在Survivor区中相同年龄的所有对象大小总和大于Survivor区的一半,年龄大于或等于该年龄的对象可以直接进入老年代。

  空间分配担保:当出现大量对象在Minor GC后仍然存活就需要老年代进行分配担保,把Survivor区无法容纳的对象直接进入老年代中。前提是老年代本身有容纳这些对象的剩余空间。

  在发生Minor GC之前,虚拟机会先检查老年代最大可用的连续空间是否大于新生代所有对象总大小或者历次晋升到老年代对象的平均大小,如果大于就进行MinorGC,否则将进行FullGC。

Guess you like

Origin www.cnblogs.com/HuiH/p/11986215.html