File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### GC回收算法
2
+ 作用:标记,处理(清除(sweep), 拷贝(copy),整理(compact))
3
+
4
+ #### 标记
5
+ 1 . 引用计数法
6
+ 优点:实现简单,判定效率高
7
+ 缺点:难以解决对象之间循环引用问题
8
+
9
+ 2 . 可达性分析算法(Roots and Reachability)
10
+ 通过"GC Roots"对象为搜索起点,搜索所走过的路径成为引用链,当对象到GC Roots没有引用链,则证明对象不可用
11
+
12
+ JAVA中可作为GC Roots的对象包括:
13
+ > 1 . 虚拟机栈中的引用对象
14
+ > 2 . 方法区中静态属性引用的对象
15
+ > 3 . 方法区中常量引用的对象
16
+ > 4 . 本地方法栈中JNI(即native方法)引用的对象
17
+
18
+ #### 处理
19
+ 1 . 标记清除算法(mark-sweep)
20
+ 1 . 标记所有需要回收的对象,在标记完成后同意回收被标记的对象
21
+ 2 . 不足:效率不高,产生碎片
22
+ 2 . 标记复制(标记拷贝)算法(mark-copy)
23
+ 1 . 事先把内存分为相等的两块内存(A,B),使用其中一块(A),gc完成时把幸存的对象整齐的拷贝到另一块(B),在继续使用(B)分配新对象,下次gc后在使用(A),以此类推
24
+ 2 . 实现简单,运行高效,但是浪费内存
25
+ 3 . 现代虚拟机中新生代中使用较多,一般默认Eden:S0: S1 = 8:1:1
26
+ 3 . 标记整理算法(mark-compact)
27
+ 1 . 标记完与复制算法一致,标记完把存活对象往一端移动,然后清楚端边界的内存
28
+ 2 . 不足:整理耗时
29
+ 3 . 老年代中使用较多
30
+
31
+ #### 分代收集器
32
+ 主要是分为串行(serial),并行(parallel),并发(concurrent)三个大的类别
33
+
34
+ ##### ** young genetation**
35
+ 1 . Serial(mark-copy): stop the world,然后单线程收集,可以配合CMS
36
+ 2 . ParNew(mark-copy): stop the world,然后多线程收集,可以配合CMS
37
+ 3 . Parallel Scavenge(mark-copy): stop the world,有限制的多线程收集
38
+
39
+ ##### ** old generation**
40
+ 1 . Concurrent Mark Sweep(as title):部分 stop the world,多线程收集
41
+ 2 . Serial Old(mark-compact): stop the world,单线程收集,CMS备选方案
42
+ 3 . Parallel Old(mark-compact): stop the world,多线程收集
43
+
44
+ ##### ** G1**
45
+ 将java heap 划分为多个 region。 整体上看 是 mark-compact 局部上看(region之间)上看 是 mark-copy
You can’t perform that action at this time.
0 commit comments