java.lang.ref.SoftReference Class in Java
Last Updated :
10 Dec, 2021
When we create an object in Java, an object isn't soft by default. To create a Soft Reference Object, we must explicitly specify this to the JVM. In Soft reference, even if the object is free for garbage collection then also it's not garbage collected until JVM is in need of memory badly. The objects get cleared from the memory when JVM runs out of memory.
Why Soft Reference Objects are used?
When a soft reference object is created, it is marked for garbage collection. However, it is not garbage collected until and unless there is a deficiency of memory in the JVM.
Constructors in the SoftReference Class :
Constructor parameters
| Constructor Description
|
---|
SoftReference ( T referent) | Creates a new soft reference that refers to the given object. |
SoftReference ( T referent, ReferenceQueue <T> q) | Creates a new soft reference that refers to the given object and is registered with the given queue. |
get() Method:
Java
// Java program to show the demonstration
// of get() method of SoftReference Class
import java.lang.ref.SoftReference;
class GFG {
public static void main (String[] args) {
// creating a strong object of MyClass
MyClass obj = new MyClass ();
// creating a weak reference of type MyClass
SoftReference<MyClass> sobj = new SoftReference<>(obj);
System.out.println ("-> Calling Display Function using strong object:");
obj.Display ();
System.out.println ("-> Object set to null");
obj = null;
// Calling the get() method
obj = sobj.get();
System.out.println ("-> Calling Display Function after retrieving from soft Object");
obj.Display ();
}
}
class MyClass {
void Display ()
{
System.out.println ("Display Function invoked ...");
}
}
Output-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from soft Object
Display Function invoked ...
Example Showing enqueue() and isEnqueued() method of SoftReference Class:
Java
// Java program demonstrating all the methods
// of SoftRefernce Class
import java.lang.ref.SoftReference;
class GFG {
public static void main (String [] args) {
// Creating object of Class X
X obj = new X ();
// Creating a soft reference of type X
SoftReference <X> softobj = new SoftReference <X> (obj);
System.out.println ("-> Retrieving object from Soft Reference using get ()");
softobj.get().show();
System.out.println ("-> Is it possible to queue object using enqueue ()");
System.out.println (softobj.enqueue ());
System.out.println ("-> Checking if reference is queued using isEnqueued ()");
System.out.println (softobj.isEnqueued ());
}
}
class X {
void show()
{
System.out.println ("show () from X invoked..");
}
}
Output-> Retrieving object from Soft Reference using get ()
show () from X invoked..
-> Is it possible to queue object using enqueue ()
false
-> Checking if reference is queued using isEnqueued ()
false
Methods Inherited from Reference Class:
Method Name | Method Description
|
---|
clear () | Clears this reference object. |
enqueue () | Adds this reference object to the queue with which it is registered, if any. |
get () | Returns this reference object's referent. |
isEqueued () | Tells whether this reference object has been enqueued, either by the program or by the garbage collector. |
Similar Reads
java.lang.ref.Reference Class in Java java.lang.ref.Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector. Cla
3 min read
java.lang.ref.WeakReference Class In Java When we create an object in Java, an object isn't weak by default. To create a Weak Reference Object, we must explicitly specify this to the JVM. Why Weak Reference Objects are used: Unlike C/C++, Java supports Dynamic Garbage Collection. This is performed when the JVM runs the Garbage Collector. In
3 min read
java.lang.ref.ReferenceQueue Class in Java A ReferenceQueue is a simple data structure onto which the garbage collector places reference objects when the reference field is cleared (set to null). You would use a reference queue to find out when an object becomes softly, weakly, or phantom reachable, so your program can take some action based
3 min read
java.lang.ref.PhantomReference Class in Java When we create an object in Java, an object is strong by default. To create a Phantom Reference Object, we must explicitly specify this to the JVM. Phantom Reference Objects are created as phantom reference object is eligible for garbage collection, but it is not collected instantly. Instead, it is
4 min read
java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
4 min read