【java.lang.ref】PhantomReference & jdk.internal.ref.Cleaner

本文详细介绍了Java中PhantomReference和jdk.internal.ref.Cleaner的使用场景、原理及优缺点。PhantomReference主要用于资源的后期清理,避免直接依赖GC回收,而Cleaner是轻量级且更健壮的资源清理工具,它使用虚引用,避免了引用对象的顺序问题。文章通过实例展示了如何利用Cleaner进行资源管理,以及其在DirectByteBuffer中的应用。然而,由于可能导致ReferenceHandler线程阻塞,Cleaner并不推荐直接使用,而应模仿其设计实现自己的资源清理机制。
摘要由CSDN通过智能技术生成

目录

  • 零、前情概要
    • ref包内容
    • 系列目录
    • 上一章回顾
  • 一、PhantomReference
    • 适用场景
    • 优雅和提前规避
      • Java中使用native memory
      • 常规做法
      • 借助于Java引用机制
    • 处理流程
  • 二、jdk.internal.ref.Cleaner
    • 源码注释
    • 源码概览
      • dummyQueue
      • add/remove
      • 为什么需要这条双向链表
      • create/thunk/clean
    • java.nio.DirectByteBuffer
    • 为什么不让你使用Cleaner
  • 总结


零、前情概 要

1.java.lang.ref包的内容

  • Reference & ReferenceQueue & ReferenceHandler 
    • SoftReference & WeakReference
    • PhantomReference
      • jdk.internal.ref.Cleaner
    • FinalReference
      • Finalizer & FinalizerThread
  • java.lang.ref.Cleaner
  • # 其中会涉及两个虚拟机线程:ReferenceHandler & FinalizerThread

2.系列目录

3.上一章回顾

  • 弱引用的适用场景和相关案例
    • 理解弱可达
    • 弱引用的处理流程
  • 软引用的适用场景
    • 理解软可达
  • 虚拟机是如何量化软引用的“内存紧张”
    • 软引用的时间戳
    • -XX:SoftRefLRUPolicyMSPerMB
    • 软引用清除策略ReferencePolicy
    • Java引用预处理ReferenceProcessor
    • 模型逻辑:正常情况、GC频繁、异常情况


一、PhantomReference

1.使用场景

API documentation:PhantomReference (Java SE 15 & JDK 15) (oracle.com)

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used to schedule post-mortem cleanup actions.

Suppose the garbage collector determines at a certain point in time that an object is phantom reachable. At that time it will atomically clear all phantom references to that object and all phantom references to any other phantom-reachable objects from which that object is reachable. At the same time or at some later time it will enqueue those newly-cleared phantom references that are registered with reference queues.

In order to ensure that a reclaimable object remains so, the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.

如API文档所述,虚引用一般用来做事后清理。为了确保虚引用的referent成为可回收对象之后不被干扰能被顺利清理掉,PhantomReference的get方法直接返回null,而不会返回referent。

这就是虚引用与软引用、弱引用的最大区别。软引用和弱引用的referent变成弱可达之后,通过其get方法还是能够让referent恢复成强可达状态,但是PhantomReference直接堵死了这条路。

除此之外,PhantomReference只有一个构造函数,必须传入ReferenceQueue,以便在referent确定会被回收之后得到一个通知。

public class PhantomReference<T> extends Reference<T> {

    /**
     * Returns this reference object's referent.  Because the referent of a
     * phantom reference is always inaccessible, this method always returns
     * {@code null}.
     *
     * @return {@code null}
     */
    public T get() {
        return null;
    }

    /**
     * Creates a new phantom reference that refers to the given object and
     * is registered with the given queue.
     *
     * <p> It is possible to create a phantom reference with a {@code null}
     * queue, but such a reference is completely useless: Its {@code get}
     * me
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值