Home  >  Article  >  Java  >  A comparative introduction to strong references, soft references, weak references and virtual references in Java

A comparative introduction to strong references, soft references, weak references and virtual references in Java

不言
不言forward
2019-02-11 11:08:402918browse

This article brings you a comparative introduction to strong references, soft references, weak references and virtual references in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Strong reference

If an object has a strong reference, the garbage collector will never reclaim it. When there is insufficient memory space, the Java virtual machine would rather throw an OutOfMemoryError error , causing the program to terminate abnormally, and will not solve the problem of insufficient memory by randomly recycling objects with strong references.

SoftReference

If there is sufficient memory space, the garbage collector will not reclaim it; if there is insufficient memory space, the memory of these objects will be reclaimed .As long as the garbage collector does not reclaim it, the object can continue to be used by the application. Soft references enable memory-sensitive caching.

Weak reference Weakreference

Objects with only weak references have a shorter life cycle. The garbage collector thread scans the memory area it governs. During the process, once an object with only weak references is discovered, its memory will be reclaimed regardless of whether the current memory space is sufficient. However, the garbage collector is a thread with a very low priority, so it may not necessarily find those objects quickly. An object with only weak references.

Virtual referencePhantomReference

Virtual reference does not determine the life cycle of the object. If an object only holds a virtual reference, then it has no Like references, they may be recycled at any time.

Reference Queue ReferenceQueue

The reference queue can be used in conjunction with soft references, weak references and virtual references. And virtual references must be used in conjunction with the reference queue. If The object referenced by a soft reference (weak reference or virtual reference) is garbage collected, and the Java virtual machine will add this reference to the reference queue associated with it.

        String reference = "hello";
        //引用队列
        ReferenceQueue<String> queue = new ReferenceQueue<>();
        //弱引用
        WeakReference<String> softReference = new WeakReference<String>(reference,queue);
        reference = null;
        //取出弱引用持有的String对象
        String str = softReference.get();
        //取出引用队列中保存的引用对象
        Reference<? extends String> poll = queue.poll();

The above is the detailed content of A comparative introduction to strong references, soft references, weak references and virtual references in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete