We know that in Java, all but the underlying data types are reference types. Java divides reference types into strong reference, soft reference, weak reference and phantom reference according to the length of their life cycle.
Normally, we only use strong reference types, and other reference types are only seen in interviews, or when reading the source code of class libraries or other frameworks.
1. Strong reference
Object obj = new Object(); When the JVM runs out of memory, it is better to throw an OutOfMemoryError that causes the program to abort than to reclaim a living object with a strong reference!
Remember that it is alive, it is not possible that if you new an object it will never be collected by GC. When a normal object has no other references, your object is not alive and can be collected by GC as long as the scope of the reference is exceeded or the reference is explicitly assigned to null. Of course, the time of collection is not always clear, depending on the GC collection strategy.
2. Soft references
Soft references have a shorter lifetime than strong references. Soft references are implemented through the SoftReference class.
Object obj = new Object(); SoftReference softObj = new SoftReference(obj); Obj = null; // Remove strong referencesCopy the code
This is a simple way to use soft references. Get the object with the get() method. When the JVM decides that it is out of memory, it goes back and tries to reclaim the object to which the soft reference points, that is, it cleans up the soft reference object before the JVM throws an OutOfMemoryError. Soft references can be used in conjunction with reference queues (ReferenceQueue).
Object obj = new Object(); ReferenceQueue queue = new ReferenceQueue(); SoftReference softObj = new SoftReference(obj,queue); Obj = null; // Remove strong referencesCopy the code
After the softObj soft reference obJ is collected by GC, the softObj object is put into the queue. After that, we can poll() the queue to check whether the object you care about was collected or return null if the queue is empty. Otherwise it returns a soft reference object, softObj.
Soft references are generally used to implement memory sensitive caches, so that if there is free memory, the cache can be retained, and when there is insufficient memory, the cache can be cleaned up, so that the cache can be used without running out of memory. For example, the image caching framework caches images through soft references.
3. A weak reference
Weak references are implemented through the WeakReference class, which has a shorter life cycle than soft references and also obtains objects through the GET () method.
Object obj = new Object(); WeakReference<Object> weakObj = new WeakReference<Object>(obj); Obj = null; // Remove strong referencesCopy the code
During GC, the object is reclaimed regardless of insufficient memory, which can also be used in conjunction with ReferenceQueue, as well as memory sensitive caches. The key in ThreadLocal uses weak references.
4. Phantom references
Also known as virtual references, this is implemented through the PhantomReference class. It can be collected by GC at any time, just as if there were no references.
Object obj = new Object(); ReferenceQueue queue = new ReferenceQueue(); PhantomReference<Object> phantomObj = new PhantomReference<Object>(obj , queue); Obj = null; // Remove strong referencesCopy the code
You cannot access any properties or functions of an object through a virtual reference. Then what’s the point of it? Virtual references simply provide a mechanism to ensure that objects are finalize to do something later. For example, when this object is recycled, it sends a system notification or something. Virtual reference must be used in conjunction with ReferenceQueue, and the specific use method is the same as soft reference mentioned above. Mainly used to track the activity of objects being garbage collected.
If there are mistakes welcome to correct!
Personal public account: Yes training level guide
There are related interview advanced materials (distributed, performance tuning, classic books PDF) waiting to be collected