Consider the following scenario: You have a singleton class that represent some sort of data provider. This singleton class allocates a lot of memory,and you want it to release it's allocated memory when there is no one using him. Flow:
How do you suggest implementing singleton that at stage 3 (class A and B "dies") will free the memory (I know that java uses garbage collection but still lets say I want the following memory = null).
PS I don't want to force each class that uses the singleton call release on singleton whenever it stops using it. I want the singleton to handle "releasing" memory by himself.
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance.
In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.
Because all the member functions of this class are static and the class has no instance variables, it is not required that the class be instantiated. The member functions can be used without an instance of the class. Thus there is no need to implement constructors or a destructor for this class.
What you can do is
WeakReference
. This will only stay alive after a GC if it is still has a "strong" reference elsewhere.If the WeakReference.get()
is null
this means it was collected because no-one was using it strongly, another weak reference doesn't count. If it is needed again you need to recreate it and the WeakReference
.
Like this,
public enum SingletonHolder{; // no instances
private static WeakReference<MyType> ref = null;
public static synchronized MyType getInstance() {
MyType type = ref == null ? null : ref.get();
if (type == null)
ref = new WeakReference<MyType>(type = new MyType());
return type;
}
}
BTW This assumes the instances which need this instance retains a reference to it. This is how the weak reference "knows" it is still needed.
BTW2 You don't need synchronized if it is single threaded but it should be harmless.
This means that you should call this method only when a new instance needs it for the first time, not every time and making it more performant shouldn't make much difference e.g. double checking just complicates it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With