当前位置:网站首页>Phantom reference virtual reference code demonstration
Phantom reference virtual reference code demonstration
2022-07-29 06:47:00 【The evil way does not miss the firewood cutting skill】
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(" Trigger gc Notify garbage collection ...");
}
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();
// When the virtual reference object is killed , Will be put into the virtual reference queue
if (poll != null) {
System.out.println("------ A virtual object entered the queue ");
}
}
}).start();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Pay attention to setting the heap memory size in advance before running the code -Xms10m -Xmx10m
Running results :
null
null
null
null
Trigger gc Notify garbage collection ...
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)
------ A virtual object entered the queue
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
The virtual reference is just to be able to notify before this object is recycled JVM A sound , Then throw this object into the queue associated with the virtual reference , Waiting to be arranged . Trigger gc Notify garbage collection ... The output indicates that the virtual reference object is recycled ,------ A virtual object entered the queue The output result indicates that the virtual reference object is placed in the virtual reference queue .
边栏推荐
- day03_ 1_ Process control
- 基于Matlab解决线性规划问题
- 9、 Networking technology
- 会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
- day02_基本语法
- 8、 Network security
- Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
- day03_2_作业
- 数据单位:位、字节、字、字长
- Huawei switch ce12808 import and export configuration file
猜你喜欢

Joint use skills of joiner.on and stream().Map

Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers

会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读

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

OpenResty的核心与cosocket

Merkletree builds QT implementation UI

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

如何画出优秀的架构图

Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions

day12_多线程
随机推荐
ping 原理
AbstractQueuedSynchronizer(AQS) 之共享锁源码浅读
损失函数——交叉熵损失函数
注解(Annotation)
将源码包转换为rpm包
find命令详解(文章最后运维最常用操作)
VMware虚拟机在物理机win10系统下如何连接外网
Relationship between subnet number, host number and subnet mask
解决分频模块modelsim下仿真输出为stx的错误
IPv6表示方法与配置案例
C语言内存-栈与堆使用
Hog+svm for pedestrian detection
3、 Wide area communication network
Leetcode question brushing record
Multiple IO usage
Online multiplayer chat room based on UDP communication
Conversion of fixed-point number to floating-point number of vivado IP core
DDoS details
7、 Next generation Internet IPv6
'function vtable for error: undefined reference to ... ' 问题的原因及解决方法