Detailed explanation of Java Reference source code analysis code
高洛峰
Release: 2017-03-19 15:37:56
Original
1746 people have browsed it
@(Java)[Reference]
Java Reference source code analysis
ReferenceObjectencapsulates references to other objects and can be operated like ordinary objects. Under certain restrictions, interaction with the garbage collector is supported. That is, you can use the Reference object to refer to other objects, but it will still be recycled by the garbage collector in the end. Programs sometimes need to be notified after an object is recycled to inform it of changes in the object's reachability. Java provides four different types of references. The reference levels from high to low are FinalReference, SoftReference, WeakReference, and PhantomReference. Among them, FinalReference is not available for external use. Each type corresponds to a different level of accessibility.
Strong reference means that there is a directly reachable reference in the program without passing any reference object , such asObjectobj =newObject();, obj is a strong reference.
Soft ReferenceSoftReference
Soft reference, not a strong reference, but can be accessed through a soft reference object. For soft reference objects, the garbage collector will decide to recycle the object pointed to by the soft reference only when there is insufficient memory (before an OOM exception is thrown). Soft references are often used to implement memory-sensitive caches.
SoftReference
Copy after login
Weak ReferenceWeakReference
Weak reference, non-strong reference and soft reference, but can be accessed through a weak reference object. A weakly referenced object will be recycled as long as it is discovered by the garbage collector, regardless of whether the memory is sufficient. For actual applications, see WeakHashMap, etc.
WeakReference weakRef = new WeakReference(new Object());
Copy after login
Virtual referencePhantomReference
Virtual reference, this reference must be used together with the reference queue (ReferenceQueue), and is generally used to track the recycling actions of the garbage collector, such as when the object is recycled , the finalize method of the object will be called. This action can be achieved using virtual references, which is also safer.
Object obj = new Object(); ReferenceQueue refQueue = new ReferenceQueue<>(); PhantomReference phantom = new PhantomReference(obj, refQueue);
Copy after login
ReferenceQueue
As a member of the reference, this queue can be used in combination with the above three reference types. The function of this queue is: when creating a Reference, register the Queue into the Reference. When the object referenced by the Reference is recycled by the garbage collector, the Reference will be placed in the queue, which is equivalent to a notification mechanism. Example Demo1:
ReferenceQueue queue = new ReferenceQueue(); WeakReference reference = new WeakReference(new Object(), queue); System.out.println(reference); System.gc(); Reference reference1 = queue.remove(); System.out.println(reference1);
Copy after login
Source code analysis
Reference and ReferenceQueue
There are several important attributes inside Reference
// 用于保存对象的引用,GC会根据不同Reference来特别对待 private T referent; // 如果需要通知机制,则保存的对对应的队列 ReferenceQueue super T> queue; /* 这个用于实现一个单向循环链表,用以将保存需要由ReferenceHandler处理的引用 */ Reference next; static private class Lock { }; // 锁,用于同步pending队列的进队和出队 private static Lock lock = new Lock(); // 此属性保存一个PENDING的队列,配合上述next一起使用 private static Reference pending = null;
ReferenceHandler is a static internal class of Reference, which is used to add Reference instances in the pending queue to different in the ReferenceQueue (depends on the queue in the Reference). The pending elements are added by the GC. Note: The pending queue is locked here. Personally, I think it is because the GC thread may execute concurrently with the thread where the ReferenceHandler is located, such as when GC uses CMS concurrent collection. As shown in the following code
The above is the detailed content of Detailed explanation of Java Reference source code analysis code. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn