一. 垃圾回收概述
二. 根搜索算法
三. 引用分类(❤❤❤)
1. 软引用:SoftReference演示
package src.com.yh.jvm.gc;
import java.lang.ref.*;
import java.util.ArrayList;
import java.util.List;
/**
* 垃圾回收-引用分类
* 对象使用
* 1. 软引用:SoftReference
* 2. 弱引用:WeakReference
* 3. 虚引用:PhantomReference
*/
public class ReferenceType {
/**
* 一种队列,用于存放User
*/
private static ReferenceQueue<User> rq = new ReferenceQueue<User>();
/**
* 验证rq队列是否释放
*/
private static void printQueue(String str) {
//获取rq队列内容
Reference<? extends User> poll = rq.poll();
if (poll != null) {
System.out.println("the gc Object Reference==" + str + " = " + poll.get());
}
}
/**
* 软引用使用User对象
* 还有用,但非必须对象
* 内存足时不会被回收,不足时会被回收
*/
private static void testSoftReference() throws InterruptedException {
List<SoftReference<User>> list
= new ArrayList<SoftReference<User>>();
//userSoftReference填充
for (int i = 0; i < 10; i++) {
//基于软引用创建对象,放入rq队列中
SoftReference<User> sr
= new SoftReference<User>(new User("soft-" + i), rq);
System.out.println("now the soft user==" + sr.get());
list.add(sr);
}
//触发垃圾回收
System.gc();
//休息一下
Thread.sleep(1000L);
printQueue("soft");
}
public static void main(String[] args) {
try {
//演示软引用GC回收
testSoftReference();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
package src.com.yh.jvm.gc;
/**
* 用于演示垃圾回收
*/
public class User {
//演示被回收,设置对象大小
private byte[] bs = new byte[1024*5];//5K
private String userId;
public User(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return "userId=='" + userId;
}
/**
* 在对象即将被回收时调用
*
* @throws Throwable
*/
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("now finalize userId==" + userId);
}
}
内存充足时
限制Xms
2. 弱引用:WeakReference
package src.com.yh.jvm.gc;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
/**
* 垃圾回收-引用分类
* 对象使用
* 2. 弱引用:WeakReference
*/
public class WeakReferenceType {
/**
* 一种队列,用于存放User
*/
private static ReferenceQueue<User> rq = new ReferenceQueue<User>();
/**
* 验证rq队列是否释放
*/
private static void printQueue(String str) {
//获取rq队列内容
Reference<? extends User> poll = rq.poll();
if (poll != null) {
System.out.println("the gc Object Reference==" + str + " = " + poll.get());
}
}
/**
* 软引用使用User对象
* 还有用,但非必须对象
* 内存足时不会被回收,不足时会被回收
*/
private static void testWeakReference() throws InterruptedException {
List<WeakReference<User>> list
= new ArrayList<WeakReference<User>>();
//userWeakReference填充
for (int i = 0; i < 10; i++) {
//基于软引用创建对象,放入rq队列中
WeakReference<User> sr
= new WeakReference<User>(new User("weak-" + i), rq);
System.out.println("now the weak user==" + sr.get());
list.add(sr);
}
Thread.sleep(1000L);
//触发垃圾回收
System.gc();
//休息一下
Thread.sleep(1000L);
printQueue("weak");
}
public static void main(String[] args) {
try {
//演示弱引用GC回收
testWeakReference();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
弱引用:WeakReference
,只要触发了垃圾回收就会被回收掉
3. 虚引用:PhantomReference
package src.com.yh.jvm.gc;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.PhantomReference;
import java.util.ArrayList;
import java.util.List;
/**
* 垃圾回收-引用分类
* 对象使用
* 3. 虚引用:PhantomReference
*/
public class PhantomReferenceType {
/**
* 一种队列,用于存放User
*/
private static ReferenceQueue<User> rq = new ReferenceQueue<User>();
/**
* 验证rq队列是否释放
*/
private static void printQueue(String str) {
//获取rq队列内容
Reference<? extends User> poll = rq.poll();
if (poll != null) {
System.out.println("the gc Object Reference==" + str + " = " + poll.get());
}
}
/**
* 软引用使用User对象
* 还有用,但非必须对象
* 内存足时不会被回收,不足时会被回收
*/
private static void testPhantomReference() throws InterruptedException {
List<PhantomReference<User>> list
= new ArrayList<PhantomReference<User>>();
//userPhantomReference填充
for (int i = 0; i < 10; i++) {
//基于软引用创建对象,放入rq队列中
PhantomReference<User> sr
= new PhantomReference<User>(new User("phantom-" + i), rq);
System.out.println("now the phantom user==" + sr.get());
list.add(sr);
}
Thread.sleep(1000L);
//触发垃圾回收
System.gc();
//休息一下
Thread.sleep(1000L);
printQueue("phantom");
}
public static void main(String[] args) {
try {
//演示虚引用:PhantomReferenceGC回收
testPhantomReference();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
四. 垃圾回收基础
1. 跨代引用
2. 垃圾判断步骤
package src.com.yh.jvm.gc;
import java.lang.ref.Cleaner;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class HelpSelf {
private static HelpSelf hs=null;
private static Integer i=0;
@Override
protected void finalize() throws Throwable {
super.finalize();
++i;
System.out.println("调用finalize()..."+i );
//自救
hs=this;
}
public static void main(String[] args) throws Exception{
hs=new HelpSelf();
//第一次
hs=null;
System.gc();
Thread.sleep(1000l);
System.out.println("第一次回收后 hs=="+hs);
//第一次
hs=null;
System.gc();
Thread.sleep(1000l);
System.out.println("第二次回收后 hs=="+hs);
}
}
运行结果
GC
第二次回收没有再调用finalize
方法了,finalize
方法只调用一次
3. GC
类型
4. 垃圾收集类型
5. 无用类判断
五. 垃圾回收算法
1. 标记清除法
2. 复制算法
3. 标记整理法
六. 垃圾收集器
1. HotSpot
2. 串行收集器
3. 并行收集器
4. 新生代PS收集器
5. CMS收集器
七. G1收集器
1. 特点
存储接口示意图:
2. 运行阶段
3. 回收过程
新生代回收过程
老年代回收过程
筛选回收
4. 使用配置(❤❤❤)
-XX:InitiatingHeapOccupancyPercent=percent
Sets the percentage of the old generation occupancy (0 to 100) at which to start the first few concurrent marking cycles for the G1 garbage collector.
By default, the initiating value is set to 45%. A value of 0 implies nonstop concurrent GC cycles from the beginning until G1 adaptively sets this value.
See also the -XX:G1UseAdaptiveIHOP and -XX:G1AdaptiveIHOPNumInitialSamples options.
The following example shows how to set the initiating heap occupancy to 75%:
-XX:InitiatingHeapOccupancyPercent=75
设置G1垃圾回收器开始前几个并发标记周期的旧一代占用率百分比(0到100)。
默认情况下,初始值设置为45%。
值为0意味着从开始到G1自适应地设置该值为止,不间断的并发GC循环。
另请参阅-XX:G1使用自适应IHOP和-XX:G1AdaptiveIHOPNumInitialSamples选项。
以下示例显示了如何将初始堆占用率设置为75%:
-XX:InitiatingHeapOccupancyPercent=75
八. ZGC收集器
1. 概述
2. GC性能指标
运行总时间=应用代码执行时间+GC运行时间+暂停时间