软引用使用
概述
软引用:内存不足时,如果GC Root只有软引用,执行gc时会被回收
例子
/**
* @Author liufq
* @Date 2023/5/27
* @Desc 软引用使用,内存不足时,如果GC Root只有软引用,执行gc时会被回收
*
* 虚拟机参数:-Xmx20m -XX:+PrintGCDetails -verbose:gc
*/
public class SoftReferenceTest {
public static void main(String[] args) {
t3();
}
public static void t1() {
List<byte[]> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new byte[4 * 1024 * 1024]);
}
}
/**
* list -> SoftReference -> byte[]
*/
public static void t2() {
List<SoftReference<byte[]>> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
System.out.println("第" + i + "次");
list.add(new SoftReference<>(new byte[4 * 1024 * 1024]));
list.forEach(e -> System.out.println(e.get()));
}
}
/**
* 关联引用队列
* gc回收软引用指向的对象后,软引用被放入引用队列中,可以遍历队列对软引用做操作
*/
public static void t3() {
List<SoftReference<byte[]>> list = new ArrayList<>();
ReferenceQueue<byte[]> queue = new ReferenceQueue();
for (int i = 0; i < 5; i++) {
System.out.println("第" + i + "次");
list.add(new SoftReference<>(new byte[4 * 1024 * 1024], queue));
list.forEach(e -> System.out.println(e.get()));
}
//list中删除引用对象已经被回收的软引用
Reference<? extends byte[]> reference = queue.poll();
int i = 0;
while (reference != null) {
list.remove(reference);
reference = queue.poll();
i++;
}
//引用队列大小为4,最后一个不需要回收
System.out.println("引用队列大小:" + i);
//剩下最后一个
list.stream().forEach(System.out::println);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具