Whether by counting algorithm to determine the number of object references, or by the root search algorithm to determine whether the object reference chain is reachable, to determine whether the object is alive or not is related to the “reference”.
References are mainly divided into four types: Strong Reference, Soft Reference, Weak Reference and Phantom Reference, and the strength of the reference.Sharp decrease in turn.
Strong reference:
This refers to a common occurrence in code, similar to “Object objectRef = new Obejct,” which will never be cleaned up by GC as long as strong references exist.
Soft reference:
Used to describe objects that are useful but not necessarily present, when the Jvm is out of memory (before memory overflows) it is reclaimed, and if the GC is executed, there is still not enough space to throw a memory overflow exception.
Soft reference is achieved through SoftReference class. SoftReference is suitable for implementing caching. In addition, when GC thinks that scanned SoftReference is not used frequently, it can be recycled.
Usage method:
-
User user = new User();
-
SoftReference<Object> softReference = new SoftReference<Object>(user);
-
softReference.get();
Weak reference
Weak references are also used to describe objects that are useful, but not necessarily present. Their strength is weakly referenced. Objects associated with weak references can only survive until the next GC. When the GC works, the weakly referenced objects are reclaimed regardless of whether the memory is sufficient. JDK through WeakRThe eference class is implemented.
When it is acquired, it can be obtained by weakReference.get and may return to null.
A ReferenceQueue object can be passed in to the WeakReference construct, and isEnqueued returns true when the reference object is represented as recoverable
-
User user = new User();
-
WeakReference<User> weakReference = new WeakReference<User>(user);
-
weakReference.get();
-
-
ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();
-
WeakReference<User> weakReference2 = new WeakReference<User>(user, referenceQueue);
-
//Returns true when the reference object is identified as recoverable, that is, true when the user object is identified as recoverable
-
weakReference.isEnqueued();
Virtual reference
Virtual references are called phantom references, and they are the weakest kind of references. Whether an object has a virtual reference or not has no effect on its lifetime. The only purpose of setting up a virtual reference Association for an object is to receive a system notification when the object is reclaimed by GC. Through PhanTomReference class implementation.
Note that the phantomReference. get method always returns null, and when the user is removed from memory, calling isEnqueued returns true
-
User user = new User();
-
ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();
-
PhantomReference<User> phantomReference = new PhantomReference<User>(user, referenceQueue);
-
//That is, when the user object identifier is recoverable, it returns true
-
System.out.println(phantomReference.isEnqueued());
-
//Always return to null
-
System.out.println(phantomReference.get());
Other related categories:
-
WeakCache weakCache = new WeakCache();
-
SoftCache softCache = new SoftCache();
-
WeakHashMap weakHashMap = new WeakHashMap();
When the garbage collection mechanism runs, the GC scans the reference relationships differently for these three types of references. Simply put, the GC first determines whether the references scanned are Reference types, if they are Reference types, and if the referenced objects are not strongly referenced.The object is considered to be of the corresponding Reference type, which is then handled by GC in garbage collection according to the different Reference types.