8.1 Weak references in Java
There are several kinds of references in Java.
There is StrongReference - these are the most common links that we create every day.
Object object = new Object();//created an object object = null;//can now be garbage collected
And there are three “special” types of links - SoftReference, WeakReference, PhantomReference. In fact, there is only one difference between all types of links - the behavior of the GC with the objects they refer to. We will discuss the specifics of each link type in more detail later, but for now, the following knowledge will suffice:
- SoftReference is a soft reference, if the GC sees that an object is only accessible through a chain of soft references, then it will remove it from memory. Maybe.
- WeakReference - a weak reference, if the GC sees that an object is only accessible through a chain of weak references, then it will remove it from memory.
- PhantomReference is a phantom reference, if the GC sees that an object is only available through a chain of phantom references, then it will remove it from memory. After several runs of GC.
You can also say that link types have a certain degree of softness:
- A regular hard link is any variable of a reference type. Not cleaned up by the garbage collector before it becomes unused.
- SoftReference . The object will not cause all memory to be used up - it is guaranteed to be deleted before an OutOfMemoryError occurs. Maybe earlier, depending on the implementation of the garbage collector.
- WeakReference . Weaker soft. Does not prevent the object from being disposed of; the garbage collector ignores such references.
- PhantomReference . Used for "death" processing of an object: the object is available after finalization until it is garbage collected.
If it is not yet clear what the difference is, then do not worry, soon everything will fall into place. The details are in the details, and the details will follow.
8.2 WeakReference and SoftReference in Java
First, let's look at the difference between WeakReference and SoftReference in Java.
In short, the garbage collector will free the memory of an object if only weak references point to it. If the object is pointed to by SoftReferences, then memory is deallocated when the JVM is in dire need of memory.
This gives a definite advantage to SoftReference over Strong reference in certain cases. For example, SoftReference is used to implement an application cache, so the first thing the JVM will do is delete objects that only SoftReferences point to.
WeakReference is great for storing metadata, such as storing a reference to a ClassLoader. If no class is loaded, then you should not refer to the ClassLoader. This is why WeakReference makes it possible for the garbage collector to do its work on the ClassLoader as soon as the last strong reference to it is removed.
WeakReference example in Java:
// some object Student student = new Student(); // weak reference to it WeakReference
weakStudent = new WeakReference (student); // now the Student object can be garbage collected student = null;
SoftReference example in Java:
// some object Student student = new Student(); // weak reference to it SoftReference
softStudent = new SoftReference (student) // now the Student object can be garbage collected // but this will only happen if the JVM has a strong need for memory student = null;
8.3 PhantomReference in Java
The PhantomReference instance is created in the same way as in the WeakReference and SoftReference examples, but it is rarely used.
A PhantomReference can be garbage collected if the object does not have strong (Strong), weak (WeakReference), or soft (SoftReference) references.
You can create a Phantom Reference object like this:
PhantomReference myObjectRef = new PhantomReference(MyObject);
PhantomReference can be used in situations where finalize() doesn't make sense. This reference type is different from other types because it is not designed to access an object. It is a signal that the object has already been finalized and the garbage collector is ready to reclaim its memory.
To do this, the garbage collector places it in a special ReferenceQueue for further processing. ReferenceQueue is where object references are placed to free memory.
Phantom references are a safe way to know that an object has been removed from memory. For example, consider an application that deals with large images. Let's say we want to load an image into memory when it's already in memory, which is ready for garbage collection. In this case, we want to wait for the garbage collector to kill the old image before loading the new image into memory.
Here PhantomReference is a flexible and safe choice. The reference to the old image will be passed to the ReferenceQueue after the old image object is destroyed. Once we have this link, we can load the new image into memory.