JVM sublimation papers

01 Garbage Collect (garbage collection)

  1.1 determine how an object is garbage?

    To carry out garbage collection, you must first know what the object is garbage.

    1.1.1 Reference counting

      For an object, just hold the object referenced in the application, it means the object is not garbage, if an object does not have any pointers reference to them, it is rubbish.
      Drawbacks: if AB holds references to each other, leading to never be recovered.
    
     1.1.2 reachability analysis 
      By the object GC Root, and started looking down to see if an object of up to 
  
It can be used as GC Root: class loader, Thread, local variable table virtual machine stack, static members, constant reference, the local method
Stack variables.

 

1.2 garbage collection algorithm
Been able to identify an object as garbage after, the next thing to consider is the recovery, how recover it?
Must have a corresponding algorithm, the following common garbage collection algorithms to talk
1.2.1 mark - Clear (Mark-Sweep)
Identify objects in memory need to be recovered, and mark them out 

 

 

Removed object is marked to be recovered, the release of the corresponding memory space 

 

 

Shortcoming
Will produce a large number of discrete memory fragmentation mark after clearing space debris could cause too much time in the future need to allocate large objects in the program is running, you can not find enough contiguous memory and had to trigger another garbage collection operation in advance.
( 1 ) mark and sweep the two processes are time-consuming and inefficient 
(
2 ) Google will produce a large number of discrete memory chips, too much space debris could cause future need to allocate large objects in the program is running, you can not find enough contiguous memory and had to trigger another garbage collection operation in advance.
1.2.2 Copy (Copying)
The memory is divided into two equal regions, each of which only one

 

 

When one piece of memory ran out, it will also copy the live objects to another one above, and then the memory space has been used once removed

 

 

Cons: reduce space utilization
 
1.2.3 mark - finishing (Mark-Compact)

The labeling process and still "mark - sweep" algorithm the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving
objects are moved to the end, then clean out the direct memory other than the end border

 

 

Let all surviving objects are moved to the end, clean out the border unexpected memory.

 

 

1.3 generational collection algorithm
Since the introduction of the 3 above garbage collection algorithm, then the heap memory in the end by which one do?
Young district: replication algorithm (the object after being assigned, it may be relatively short life cycle, replication efficiency Young area is relatively high)
Old district: mark or remove tags to organize (Old zone objects survive a long time, no need to copy to copy, it is better to be labeled and then clean up)
1.4 garbage collector
If the algorithm is a collection methodology memory recovery, then the garbage collector garbage collection is a concrete realization, it means landing slightly.

 

 

1.4.1 Serial Collector 

Serial收集器是最基本、发展历史最悠久的收集器,曾经(在JDK1.3.1之前)是虚拟机新生代收集的唯
一选择。
它是一种单线程收集器,不仅仅意味着它只会使用一个CPU或者一条收集线程去完成垃圾收集工作,更
重要的是其在进行垃圾收集的时候需要暂停其他线程

优点:简单高效,拥有很高的单线程收集效率 
缺点:收集过程需要暂停所有线程 
算法:复制算法
适用范围:新生代 
应用:Client模式下的默认新生代收集器

 

 

1.4.2 ParNew收集器 
可以把这个收集器理解为Serial收集器的多线程版本。
优点:在多CPU时,比Serial效率高。
缺点:收集过程暂停所有应用程序线程,单CPU时比Serial效率差。
算法:复制算法
适用范围:新生代
应用:运行在Server模式下的虚拟机中首选的新生代收集器

 

 

1.4.3 Parallel Scavenge收集器

Parallel Scavenge收集器是一个新生代收集器,它也是使用复制算法的收集器,又是并行的多线程收集
器,看上去和ParNew一样,但是Parallel Scanvenge更关注 系统的吞吐量 。

吞吐量=运行用户代码的时间/(运行用户代码的时间+垃圾收集时间)
比如虚拟机总共运行了100分钟,垃圾收集时间用了1分钟,吞吐量=(100-1)/100=99%
若吞吐量越大,意味着垃圾收集的时间越短,则用户代码可以充分利用CPU资源,尽快完成程序
的运算任务。
-XX:MaxGCPauseMillis控制最大的垃圾收集停顿时间,
-XX:GCTimeRatio直接设置吞吐量的大小。
1.4.4 Serial Old收集器

Serial Old收集器是Serial收集器的老年代版本,也是一个单线程收集器,不同的是采用"标记-整理算
法",运行过程和Serial收集器一样

 

 

1.4.5 Parallel Old收集器 

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

吞吐量优先
1.4.6 CMS收集器
CMS(Concurrent Mark Sweep)收集器是一种以获取 最短回收停顿时间 为目标的收集器。
采用的是"标记-清除算法",整个过程分为4步
(1)初始标记 CMS initial mark 标记GC Roots能关联到的对象 Stop The World-- ->速度很快
(2)并发标记 CMS concurrent mark 进行GC Roots Tracing
(3)重新标记 CMS remark 修改并发标记因用户程序变动的内容 Stop The World 
(4)并发清除 CMS concurrent sweep

采用的是"标记-清除算法",整个过程分为4步由于整个过程中,并发标记和并发清除,收集器线程可以与用户线程一起工作,所以总体上来
说,CMS收集器的内存回收过程是与用户线程一起并发地执行的。

优点:并发收集、低停顿 
缺点:产生大量空间片、并发阶段会降低吞吐量

 

 

1.4.7 G1收集器 
G1特点 
并行与并发 
分代收集(仍然保留了分代的概念) 
空间整合(整体上属于“标记-整理”算法,不会导致空间碎片) 
可预测的停顿(比CMS更先进的地方在于能让使用者明确指定一个长度为M毫秒的时间片段内,消耗在垃圾收集 上的时间不得超过N毫秒)
使用G1收集器时,Java堆的内存布局与就与其他收集器有很大差别,它将整个Java堆划分为多个
大小相等的独立区域(Region),虽然还保留有新生代和老年代的概念,但新生代和老年代不再
是物理隔离的了,它们都是一部分Region(不需要连续)的集合
工作过程可以分为如下几步 
初始标记(Initial Marking) 标记一下GC Roots能够关联的对象,并且修改TAMS的值,需要暂 停用户线程 
并发标记(Concurrent Marking) 从GC Roots进行可达性分析,找出存活的对象,与用户线程并发 执行
最终标记(Final Marking) 修正在并发标记阶段因为用户程序的并发执行导致变动的数据,需 暂停用户线程 
筛选回收(Live Data Counting and Evacuation) 对各个Region的回收价值和成本进行排序,根据 用户所期望的GC停顿时间制定回收计划

 

 

1.4.8 垃圾收集器分类
串行收集器->Serial和Serial Old 
只能有一个垃圾回收线程执行,用户线程暂停。 适用于内存比较小的嵌入式设备 。
并行收集器[吞吐量优先]->Parallel Scanvenge、Parallel Old
多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态。 适用于科学计算、后台处理等若交互场景 
并发收集器[停顿时间优先]->CMS、G1 
用户线程和垃圾收集线程同时执行(但并不一定是并行的,可能是交替执行的),垃圾收集线程在执行的
时候不会停顿用户线程的运行。 适用于相对时间有要求的场景,比如Web 。
1.4.9 理解吞吐量和停顿时间 

停顿时间->垃圾收集器 进行 垃圾回收终端应用执行响应的时间
吞吐量->运行用户代码时间/(运行用户代码时间+垃圾收集时间)

停顿时间越短就越适合需要和用户交互的程序,良好的响应速度能提升用户体验; 
高吞吐量则可以高效地利用CPU时间,尽快完成程序的运算任务,主要适合在后台运算而不需要太多交互的任 务。
小结 :这两个指标也是评价垃圾回收器好处的标准,其实调优也就是在观察者两个变量。 
 
1.4.10 如何选择合适的垃圾收集器 

官网 :https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/collectors.html#sthr
ef28

优先调整堆的大小让服务器自己来选择
如果内存小于100M,使用串行收集器
如果是单核,并且没有停顿时间要求,使用串行或JVM自己选
如果允许停顿时间超过1秒,选择并行或JVM自己选
如果响应时间最重要,并且不能超过1秒,使用并发收集器
对于G1收集
1.4.11 再次理解G1
JDK 7开始使用,JDK 8非常成熟,JDK 9默认的垃圾收集器,适用于新老生代。
判断是否需要使用G1收集器? 
150%以上的堆被存活对象占用 2)对象分配和晋升的速度变化非常大3)垃圾回收时间比较长
1.4.12 如何开启需要的垃圾收集
1)串行 -XX:+UseSerialGC -XX:+UseSerialOldGC 2)并行(吞吐量优先): -XX:+UseParallelGC -XX:+UseParallelOldGC 
3)并发收集器(响应时间优先) -XX:+UseConcMarkSweepGC -XX:+UseG1G咕

 

Guess you like

Origin www.cnblogs.com/flgb/p/12064763.html
JVM
JVM