首页 > 其他 > 详细

垃圾回收

时间:2020-09-07 08:14:07      阅读:66      评论:0      收藏:0      [点我收藏+]

官方调优建议:https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/

一、堆分配

Default Arrangement of Generations, Except for Parallel Collector and G1

技术分享图片

Arrangement of Generations in the Parallel Collector

技术分享图片

 

Heap Division by G1

 

技术分享图片

二、垃圾回收中用到的算法

  1.Root Searching 根可达算法

  FollowReferences(jvmtiEnv* env,
            jint heap_filter,
            jclass klass,
            jobject initial_object,
            const jvmtiHeapCallbacks* callbacks,
            const void* user_data)

  This function initiates a traversal over the objects that are directly and indirectly reachable from the specified object or, if initial_object is not specified, all objects reachable from the   heap roots.The heap root are the set of system classes, JNI globals, references from thread stacks, and other objects used as roots for the purposes of garbage collection.

  引用自https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html

  通过其他渠道获取的说法为,如下情况的对象可以作为GC Roots:

    1. 虚拟机栈(栈桢中的本地变量表)中的引用的对象
    2. 方法区中的类静态属性引用的对象
    3. 方法区中的常量引用的对象
    4. 本地方法栈中JNI(Native方法)的引用的对象

  2.mark-sweep

  容易产生碎片,多用于老年代

  3.copying

  会产生空间浪费,多用于新生代

  4.mark-compact

  需要移动对象,效率相对较低,多用于老年代

  5.三色标记

   黑色:根对象,或者该对象与它的子对象都被扫描过。
        灰色:对本身被扫描,但是还没扫描完该对象的子对象。
        白色:未被扫描对象,如果扫描完所有对象之后,最终为白色的为不可达对象,既垃圾对象。

  三色标记的问题,如果在标记过程中引用关系发生变化,会导致白色对象漏标,被错误回收。

  解决方案:CMS采用了Increamental Update 当白色对象被黑色对象引用时,重新将黑色对象标记为灰色,让collector重新扫描

       G1采用了SATB(snapshot at the beginning),维护一个快照表,将被删除的引用放到GC的堆栈中,保证还能被gc扫描到。 

三、常见垃圾回收器

  1. Serial

  2. Parrallel Scavenge

  3. ParNew

  4. SerialOld

  5. ParallelOld

  6. ConcurrentMarkSweep

  三色标记 + Incremental Update

  7. G1

  三色标记 + SATB+CSet+RSet+Card Table

  8. ZGC

  ColoredPointers + LoadBarrier

四、常见垃圾回收器组合参数设定(1.8)

  HotSpot会根据计算及配置和JDK版本自动选择收集器

  • -XX:+UseSerialGC = Serial New (DefNew) + Serial Old

  • -XX:+UseConc(urrent)MarkSweepGC = ParNew + CMS + Serial Old

  • -XX:+UseParallelGC = Parallel Scavenge + Serial Old(1.8默认)    高吞吐量

  • -XX:+UseParallelOldGC = Parallel Scavenge + Parallel Old

  • -XX:+UseG1GC = G1 低停顿

垃圾回收

原文:https://www.cnblogs.com/macbk/p/13624500.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!