当前位置:网站首页>PhantomReference 虚引用代码演示
PhantomReference 虚引用代码演示
2022-07-29 05:27:00 【魔道不误砍柴功】
package com.xxl.job.admin.mytest;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ReferenceDemo {
@Override
protected void finalize() {
System.out.println("触发 gc 通知垃圾回收...");
}
public static void main(String[] args) {
ReferenceQueue<ReferenceDemo> referenceQueue = new ReferenceQueue<>();
PhantomReference<ReferenceDemo> reference = new PhantomReference<>(new ReferenceDemo(), referenceQueue);
List<byte[]> list = new ArrayList<>();
new Thread(()->{
while(true) {
list.add(new byte[1*1024*1024]);
try {
TimeUnit.SECONDS.sleep(1);
System.out.println(reference.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
while(true) {
Reference<? extends ReferenceDemo> poll = referenceQueue.poll();
// 当这个虚引用的对象被干掉了,就会放进虚引用队列
if (poll != null) {
System.out.println("------有虚对象进入了队列");
}
}
}).start();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
注意运行代码之前提前设置下堆内存大小 -Xms10m -Xmx10m
运行结果:
null
null
null
null
触发 gc 通知垃圾回收...
null
null
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at com.xxl.job.admin.mytest.ReferenceDemo.lambda$main$0(ReferenceDemo.java:25)
at com.xxl.job.admin.mytest.ReferenceDemo$$Lambda$1/517380410.run(Unknown Source)
at java.lang.Thread.run(Thread.java:750)
------有虚对象进入了队列
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
虚引用只是为了能够在这个对象被回收之前通知下 JVM 一声,然后再把这个对象丢进虚引用关联的队列中,等待被安排。触发 gc 通知垃圾回收... 输出结果表示这个虚引用对象被回收,------有虚对象进入了队列 输出结果表示将虚引用对象放入虚引用队列中。
边栏推荐
- 软件测试职业发展:软件测试人员该何去何从
- Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction
- 浅谈缺陷描写样式
- FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
- Network Security Learning (I)
- Leetcode刷题记录
- day03_1_流程控制
- Understand the great changes of network security in five years
- day03_ 1_ Process control
- etcd原理
猜你喜欢

Merkle tree existential function modified for the first time

Design and simulation code of 4-bit subtracter based on FPGA

etcd原理

6、 Network interconnection and Internet

day09_ Static & Final & code block & abstract class & Interface & internal class

day03_ 1_ Process control

day13_ Under multithreading

C语言内存-栈与堆使用

What are the advantages of software testing? See how much you know

After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
随机推荐
6、 Network interconnection and Internet
day04_ array
Several misunderstandings about DDoS
Phishing mail disposal
What if the 80443 port of the website server has been maliciously attacked?
Clickhouse failed to import CSV without error but no data
4、 LAN and man
IPv6表示方法与配置案例
Day16 set
四、 局域网和城域网
day13_ Under multithreading
什么是DNS放大攻击
Network Security Learning (II)
20个hacker神器
How to use SFTP command to access SFTP server on the development board
How to judge whether a business is attacked by DDoS? What harm will it cause?
DDoS details
10种常见的软件架构模式
What are the advantages of software testing? See how much you know
超低成本DDoS攻击来袭,看WAF如何绝地防护