Four references to java objects: strong, soft, weak, and virtual

Introduction

The java garbage collection mechanism uses two methods to determine whether an object is reclaimed, namely, the reference counting method and the reachability analysis algorithm

Before jdk1.2, there is only one kind of reference. When the object is referenced, it will exist. When there is no reference, the reference count is 0, and the object will be judged as invalid by garbage collection.

Java started with jdk1.2. There are four types of object references, namely strong references>soft references>weak references>virtual references . Programmers can control the life cycle of objects through different references to facilitate garbage collection and make the program more flexible. Control object life cycle

Insert picture description here
Java started in jdk1.2, and provided an abstract class Reference and three implementation classes SoftReference ( soft reference ), WeakReference ( weak reference ), PhantomReference ( virtual reference ) in java.lang.ref

Strong citation

/**
     * 强引用
     */
    public void testStrongReference(){
    
    
        //强引用
        Object obj=new Object();
    }

Most of the objects in the business code are strong references, such as Object obj=new Object(). Such referenced objects will never be garbage collected . When the memory is not enough to allocate the memory for creating the object, the Java virtual machine will throw An OOM error causes the program to terminate abnormally, and this object is not recycled. Only after the reference disappears (* Explicitly assigned to null*, the method life cycle ends, etc.), the reference count will be 0 before it will be garbage collected

Soft reference

/**
     * 软引用
     */
    public void testSoftReference(){
    
    
        //强引用
        Object obj=new Object();
        //放入SoftReference
        SoftReference<Object> softReference = new SoftReference<Object>(obj);
        System.out.println( softReference.get());//java.lang.Object@39a054a5
    }

Soft references are one level lower than strong references. They are used to represent useful and non-essential objects. Only when the memory is insufficient, the soft reference objects will be garbage collected , which can be used to achieve caching without worrying about memory overflow.

/**
     * 软引用(引用队列)
     */
    public void testSoftReferenceQueue(){
    
    
        //引用队列
        ReferenceQueue referenceQueue = new ReferenceQueue<>();

        //使用守护线程
        Thread thread= new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    int i=0;
                    SoftReference<Integer> softReference;
                    //如果对象被gc回收
                    while((softReference = (SoftReference) referenceQueue.remove()) != null) {
    
    
                        System.out.println("第"+i+"次回收:"+softReference);
                        i++;
                    }
                }catch (Exception e){
    
    

                }
            }
        });
        thread.setDaemon(true);//设置为守护线程,监听对象变化
        thread.start();

        Map<Object, Object> map = new HashMap<>();
        Object obj=new Object();
        for(int i=0;i<2000; i++){
    
    
            //放入SoftReference转为软引用
            byte [] bytes=new byte[1024*1024];
            SoftReference<byte []> softReference = new SoftReference<byte []>(bytes,referenceQueue);
            //将软引用作为key
            map.put(softReference,obj);
        }
    }

第1992次回收:java.lang.ref.SoftReference@7c7a06ec
第1993次回收:java.lang.ref.SoftReference@1d296da
第1994次回收:java.lang.ref.SoftReference@776aec5c
[GC (Allocation Failure) [PSYoungGen: 30K->64K(1536K)] 3833K->3866K(5632K), 0.0002270 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 64K->64K(1536K)] 3866K->3866K(5632K), 0.0002042 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 64K->0K(1536K)] [ParOldGen: 3802K->3802K(4096K)] 3866K->3802K(5632K), [Metaspace: 3120K->3120K(1056768K)], 0.0019695 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3802K->3802K(5632K), 0.0002220 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3802K->730K(4096K)] 3802K->730K(5632K), [Metaspace: 3120K->3120K(1056768K)], 0.0025588 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
The 1995 recovery: java.lang.ref.SoftReference@408d971b
The 1996 recovery: java.lang.ref.SoftReference@75d4a5c2
The 1997 recovery: java.lang.ref.SoftReference@557caf28

The soft reference is implemented by SoftReference. The soft reference object can be registered in the reference queue (Referencequeue), and the reference queue is used to determine whether the object is released. Reference has a next field. There are four states that can be judged according to the state, but the next is only in Put it in the queue will make sense (because, only when enqueue, next will be set to the next Reference object to be processed)
Insert picture description here
active: the state when the memory is allocated.
pending: The state when the memory is about to be reclaimed and stored in the associated reference queue.
Enqueued: When the memory is reclaimed, it enters the state in the reference queue.
Inactive: Inactive state.

Weak reference

/**
     * 弱引用
     */
    public void testWeakReference(){
    
    
        //强引用
        Object obj=new Object();
        //放入WeakReference
        WeakReference<Object> weakReference = new WeakReference<Object>(obj);
        System.out.println(weakReference.get());//java.lang.Object@39a054a5
    }

Weak references are the first level than soft references. They are used to represent non-essential objects . Regardless of whether the memory is sufficient, their objects will be garbage collected. However, because the priority of the garbage collection thread is very low,
weak references will not be quickly collected through WeakReference. For implementation, the weak reference object can be registered in the reference queue (Referencequeue), and the reference queue can be used to determine whether the object is released. If you want to test, you can use the thread monitoring method weakly referenced above

Phantom reference

/**
     * 虚引用
     */
    public void testPhantomReference(){
    
    
        //强引用
        Object obj=new Object();
        //必须使用引用队列
        ReferenceQueue referenceQueue = new ReferenceQueue();
        //放入PhantomReference
        PhantomReference<Object> phantomReference = new PhantomReference<Object>(obj,referenceQueue);
        System.out.println(phantomReference.get());//null 因为虚引用无法获取对象实例,只是监听作用
        System.out.println(obj);//java.lang.Object@39a054a5
    }

Virtual references
Compared with other references, virtual references are just like virtual references. Object instances cannot be obtained through virtual references. If an object is only held by virtual references, it will be garbage collected at any time . Unlike soft references and weak references, it must Registered in the Referencequeue , when the object is about to be recycled, it will put the virtual reference into the reference queue, and judge whether the object should be recycled by judging whether the virtual reference is added to the reference queue

Guess you like

Origin blog.csdn.net/qq_38306425/article/details/106685465