JVM 可达性

1 篇文章 0 订阅

可达性概述

  1. 当一个对象到 GC ROOT 不存在任何一条引用链的时候,表示此对象不可达,此对象会被判定为可回收对象。
  2. 以 GC ROOT 作为根节点,向下搜索走过的路径为引用链。

在这里插入图片描述

谁可以做 GC ROOT?

  1. 虚拟机栈中引用的对象
  2. 方法区中类静态属性引用的对象
  3. 方法区中常量引用的对象
  4. 本地方法区中 JNI 引用的对象

环状引用如何什么情况被回收?

JVM 计数器针对环装引用到达一定计数会执行回收。

引用类型分析

强引用

// 如果强引用一直存在,则对象不会被回收。
Object object=new Object();

软引用:还有用但是并非必须的对象

// 如果出现内存不足的时候,会 GC 软引用对象
// 继承下方法实现软引用对象
public class SoftReference<T> extends Reference<T> {
 
    /**
     * Timestamp clock, updated by the garbage collector
     */
    static private long clock;
 
    /**
     * Timestamp updated by each invocation of the get method.  The VM may use
     * this field when selecting soft references to be cleared, but it is not
     * required to do so.
     */
    private long timestamp;
 
    /**
     * Creates a new soft reference that refers to the given object.  The new
     * reference is not registered with any queue.
     *
     * @param referent object the new soft reference will refer to
     */
    public SoftReference(T referent) {
        super(referent);
        this.timestamp = clock;
    }
 
    /**
     * Creates a new soft reference that refers to the given object and is
     * registered with the given queue.
     *
     * @param referent object the new soft reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     *
     */
    public SoftReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
        this.timestamp = clock;
    }
 
    /**
     * Returns this reference object's referent.  If this reference object has
     * been cleared, either by the program or by the garbage collector, then
     * this method returns <code>null</code>.
     *
     * @return   The object to which this reference refers, or
     *           <code>null</code> if this reference object has been cleared
     */
    public T get() {
        T o = super.get();
        if (o != null && this.timestamp != clock)
            this.timestamp = clock;
        return o;
    }
 
}

弱引用:比软引用更弱,被弱引用关联的对象存在周期是下次 GC 之前,和内存充足没有关联。

// 实现类
public class WeakReference<T> extends Reference<T> {
 
    /**
     * Creates a new weak reference that refers to the given object.  The new
     * reference is not registered with any queue.
     *
     * @param referent object the new weak reference will refer to
     */
    public WeakReference(T referent) {
        super(referent);
    }
 
    /**
     * Creates a new weak reference that refers to the given object and is
     * registered with the given queue.
     *
     * @param referent object the new weak reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public WeakReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }
 
}

虚引用:最弱的一种引用方式,存在对生存时间没有影响,为对象设置虚引用关联的目的是能在对象被收集器回收的时候收到系统通知。

PhantomReference.java
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值