JVM Learning Series (b) Garbage collection

How to determine whether the object recyclable

Reference counting

1, the concept of: adding a reference to the object counter, whenever a reference to his place when the counter value of +1, when the failure of the reference, counter -1 at any time the counter for the object 0 is not being used Object.

2, disadvantages: the object can not be resolved in a circular reference (below)
u3aqFP.png

Reachability analysis

1, the concept of: garbage collection root (GCRoot) searches down the search path traversed chain called a reference, when an object has no references to the chain GCRoot time, represents the current object is not available.

2, GCRoot included objects:

  • Virtual Machine stack (local variable stack frame table) in the object reference
  • Method zone class object referenced by static properties
  • Methods district constants referenced objects
  • Native method stacks lock object referenced

3, citing the concept of

  • Strong Quote: garbage collector never recovered off the referenced object; Object obj = new Object ();
  • Soft reference: system memory before the overflow is to occur, such objects will be classified and subjected to the recovery range second recovered; the SoftReference
  • Weak references: strength weaker than the soft references, is associated with a weak reference to an object can only survive until the next garbage collection occurs; WeakReference
  • Phantom reference: whether the existence of a phantom reference object is completely not affect their survival time, can not get a reference to the object instance through the virtual, virtual object is set to a reference system that is associated purpose can be collected at this target to give a notification recovery system; the PhantomReference

Collection algorithm

Clear labeling algorithm

1, concepts: mark all objects need to be recovered, marking the completion of reunification after the recovery of the lost object is marked.
2. Disadvantages:

  • Efficiency: labeling and cleaning efficiency is not high
  • Space problem: will produce large amounts of discrete memory fragmentation, space debris could cause too much can not find enough contiguous memory needs to be allocated a larger object program is running and had to trigger another garbage collection operation in advance

Replication algorithm

1, the concept: the available memory capacity is divided into equal size by two, each time only one of them, when it runs out of memory a copy live objects to another above and which has been used memory space once cleaned up;

2, Advantages: algorithm to solve the memory fragmentation problems that exist clear mark

3, Disadvantages:

  • Waste of space: the memory will be reduced to half the original, low memory use efficiency.
  • 对象存活率较高时就要执行多次的复制算法,效率变低。
  • 只能用于新生代,无法用户老年代这种对象存活率较高的情况
    3、复制算法优化:
    用该算法回收新生代,将内存分为一块较大的Eden和两块Survivor空间(HotSpot默认比例是8:1),每次使用Eden和其中的一块Survivor空间,当回收时,将Eden和Survivor中还存活的对象一次性拷贝到另外一个Survivor空间上,最后清理掉Eden和刚才用过的Survivor空间,这样只会浪费10%的空间;如果另外一个Survivor空间没有足够的空间存放上一次新生代收集下来的存活对象,这些对象将直接通过分配担保机制进入老年代。

标记整理算法

1、概念
过程与标记清除算法一致,但后续在清理的时候不是直接对可回收的对象进行清理,而是让所有存活的对象都向另外一端移动,然后直接清理掉端边界以外的内存

2、优点:老年代可以用,可以解决标记复制算法在对象存活较高时效率低下的问题

分代收集算法

1、概念:根据对象的存活周期将内存划分为几块,JAVA堆分为新生代和老年代,根据各个年代的特点采用适当的收集算法,新生代收集时大量对象被收集,小亮对象存活,所有采用复制算法,老年代存活率高,没有额外的空间对它进行分配担保,可以用标记清理或者标记整理算法。

垃圾回收器

Serial

1、概念:最基本收集器,新生代收集的唯一选择,单线程收集器

2、优点:相对于其他收集器的单线程比,没有线程交互的开销,简单切高校

3、缺点:在进行垃圾收集时,会暂停其他的工作线程(Stop The World)

Parnew

1、概念:Serial收集器的多线程版本

2、优点:除了Serial收集器外,只有它能与CMS收集器配合使用

3、缺点:还是存在Stop The World的问题,只不过比Serial稍微好点

4、相关参数:

  • -XX:+UseParNewGc 使用parnew收集器
  • -UseConcMarkSweepGc 使用该选项时,会默认将该收集器做为新生代的收集器

    • 并行:指多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态
    • 并发:用户线程和垃圾收集线程同时执行;

Parallel Scavenge 收集器

1、概念:新生代收集器,使用复制算法的并行多线程收集器;目标是达到一个可控制的吞吐量,适用于后台运算不需要太多交互的任务;该收集器可以与Serial Old配合使用

2、缺点:无法和CMS收集器一起使用

3、相关参数:

  • -XX:MaxGcPauseMillis 控制最大垃圾收集停顿时间(大于0的毫秒数),该值越小吞吐量越低,相对的
  • -XX:GCTimeRatio 吞吐量大小 参数值(垃圾收集时间占总时间的比率)大于0小与100的整数,正常是吞吐量的倒数
  • -XX:+UseAdaptiveSizePolicy 打开后,不需要手动指定新生代的大小(-Xmn)、Eden和Survivor的比例(-XX:SurvivorRation)、晋升老年代的对象年龄(-XX:PretenureSizeThreshold)等参数,虚拟机回根据当前系统的运行情况收集戏能健康信息,动态调整来提供合适的停顿时间或者是吞吐量

    吞吐量:CPU用户运行代码的时间与CPU总消耗时间的比值,吞吐量=运行代码时间/(运行用户代码时间+垃圾收集时间)

Serial Old 收集器

1、概念:Serial收集器的老年代版本,单线程,采用标记整理算法

Parallel Old收集器

1、概念:Parallel Scavenge收集器的老年代版本,多线程,采用标记整理算法;在注重吞吐量和CPU资源敏感的情况下,可以与Parallel Scavenge 配合使用

CMS(Concurrent Mark Sweep)收集器

1、概念:一种以获取最短回收停顿时间为目标的收集器,基于标记清除算法实现的;默认开启回收线程数是(CPU+3)/4
2、运作过程:

  • 初始标记:
  • 并发标记:
  • 重新标记:
  • 并发清除:

3. Advantages:

  • Concurrent collection
  • Lower standstill

4. Cons:

  • CPU resources sensitive: Although not Stop the world, but will take up part of the threads lead to slower user program, the throughput is low. When the CPU when four or more, collection thread will not take up more than 25%, less than 4, the impact is very big;
    • Optimization approach: incremental concurrent collector: i-CMS, and published in time to clean up the base and allow concurrent GC threads and user threads run alternately, reduce the time GC threads exclusive resources, this may result in the collection side length of time, but less impact on the program
  • Floating garbage: Because the presence of floating garbage, the situation Concurrent Mode Failure may occur. Due to the nature of the CMS user thread continues to run garbage collection stage must be set aside enough memory space for user threads, so the CMS can not be the same as with other collectors, and other old's completely full and we have to start collecting, you need to set aside part of the space operational procedures used to provide concurrent collection. By default, the old year occupation of 68% will be activated collected; this can -XX: CMSInitiatingOccupancyFraction do adjustment;
  • Space debris problem: Because the tag-based algorithm cleared, it will cause problems with debris
    • Optimized way: -XX: + automatically compressed debris UseCMSCompackAtFullCollection fullGC after completion (resulting in longer dwell time); - XX: How many times after CMSFullGCsBrforeCompaction uncompressed fullGC settings, perform a compression FullGC

      • Concurrent clean up at the same time because they do not Stop The world will lead to a new garbage at the same time, this part is called the garbage floating garbage;
      • ConcurrentMode Failure: When reserving memory can not meet the needs of running, Concurrent Mode Failture will happen, it will temporarily adopt the provisional start Serial Old to collect after the occurrence, which can lead to a longer dwell time

G1 collector

1, concepts: Based tags to organize algorithm
2 advantages:

  • No debris
  • You can be precisely controlled standstill

Guess you like

Origin www.cnblogs.com/jakaBlog/p/11767885.html