Reputation: 6335
I declared a strong reference
-
Data data = new Data();
To make it eligible for garbage collection I made instance null-
data = null;
Now when I made reference null the object is not reachable and is garbage collected when GC runs.
And for using WeakReference
-
Data data = new Data();
WeakReference<Data> weakRef = new WeakReference<Data>(data);
When I make reference variable null it will be eligible for garbage collection.
data = null;
When we use StrongReference we made its reference null to get it GC. In case of WeakRefernce also we need to make reference to object null to make it GC. What is difference between two. In case of Strong reference if we just make reference to object null it will eligible for GC and will be collected when GC runs.
Upvotes: 1
Views: 965
Reputation: 5316
Though the post already has some good answers ,there is just a little left which I would like to add. In the question it has been asked for
"When we use StrongReference we made its reference null to get it GC. In case of WeakRefernce also we need to make reference to object null to make it GC. What is difference between two ?".
Suppose we have an Object which is referenced by 2 strong references , x
and y
. if we make x = null
the object that was being referenced by x
is not yet eligible for garbage collection as still we have y
referencing the same. Reading y
will still result in outputting the not null object.
Now suppose we have an Object having one strong reference, say x
and one weakreference, y
. Now if we make x = null
we can be sure that the object will be eligible for garbage collection. WeakReference will not prevent this object from being garbage collected.
Invoking the get()
method on the weak reference may return null
as soon as the object is garbage collected. Hence it is advisable to always check the returned object before use.
(Important) Also we need to make that we do not hold the returned non null value in any long living hard reference (like instance fields
). If we get a weak reference then if we need then we should use weak reference as instance field and to get the object in any method, a local variable may be used to hold the returned object, it should be checked if its not null, and if so then further processing can be done using the object.
Now just an additional point for SoftReferences, they prevent the object from being garbage collected untill there is an low memory (below a threshold).
Upvotes: 0
Reputation: 136042
The difference is that you can know if GC collected data by calling weakRef.get() and testing if it returned null or not.
Upvotes: 1
Reputation: 20628
If you have tangled objects (i.e. objcts that are not reachable anymore), the garbage collector is allowed to collect them and wipe them out. Note the wording: It is allowed. That means the garbage collector can decide not to collect them. In fact, it is not guaranteed that an object will be garbaged collected at all.
The difference between objects managed by a SoftReference or a WeakReference and a removed strong reference is the garbage collection strategy:
Still, these are assumptions about the most probable strategy, but a garbage collector might decide in another way.
Just a note: Weak and soft references do not make sense when being used in a local scope. They are most probably used in instance fields, and one must always take care of the fact that the referred-to object might already be collected.
Upvotes: 3