【JVM】【02】垃圾收集

1.垃圾收集的算法

1.标记-清理
mark-sweep
2.复制算法
copying
3.标记-压缩
mark-compact

2.如何标记垃圾

https://www.jianshu.com/p/12544c0ad5c1

可达性算法
从GC root出发,能被引用到的都不是垃圾,其他的都需要被回收
GC root包含栈的局部变量表中引用、本地方法栈的引用、常量池、类变量的引用变量

三色标记的三种颜色
黑色表示对象和成员变量都被标记了
灰色表示对象被标记了,但是成员变量还没有标记
白色表示对象还没有被标记

算法执行过程
先从GC root出发
如果对象没有别的引用了就变成黑色的放到黑色集合中
如果还有成员变量引用把所有的直接关联对象标成灰色,放到灰色集合中
循环灰色集合中的对象,只到灰色对象全部标记变成黑色对象,完成标记

【JVM】【02】垃圾收集_第1张图片

3.对象分配过程

1先栈上分配空间,如果栈上分配不了在堆上分配

2.TLAB
TheadLocalAllocationBuffer
减少多个线程抢占地址的资源浪费,
Eden区每个线程都分配一块小的空间
这个空间叫做TLAB

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

-XX:+UseTLAB
Enables the use of thread-local allocation blocks (TLABs) in the young generation space. 
This option is enabled by default. To disable the use of TLABs, specify -XX:-UseTLAB.

-XX:TLABSize=size
Sets the initial size (in bytes) of a thread-local allocation buffer (TLAB). 
Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. 
If this option is set to 0, then the JVM chooses the initial size automatically.

The following example shows how to set the initial TLAB size to 512 KB:
-XX:TLABSize=512k

-XX:TLABRefillWasteFraction

3.分代垃圾回收器

serial    parallel scavenge  parnew
-------------------------------
CMS     serial old      parallel Old 

4.CMS

initial mark 标记出GC root对象
concurrent mark 三色标记法
remark 重新标记
concurrent sweep 并发清理

5.G1垃圾回收

1.逻辑上分代,物理上不分代,物理上时分区的

2.youngGC和混合GC
当堆内存不到百分之45时做youngGC,当堆内存到百分之45时发生mixedGC
mixed GC时标记 部分移动的算法

3.使用时设置最大暂停时间,mixedGC的阈值就可以了,算法会根据最大暂停时间选择到底GC多少个区

你可能感兴趣的:(java)