Strong Reference - As long as if application refers to the object. GC will not collect those objects. Those objects we say are strong reference objects.
Weak Reference - Even though application refers to the object, GC may collect those object or may not be. Those objects are weak referenced objects.
Confused ? Let me explain in brief -
We all know GC is a non deterministic background process so we do not know when it will be called. So only weak referenced objects may reside in the memory for the long time or may not be. So it's up to us whether we want our weak references in memory or not. Lets look over the below examples -
Weak references are of two types -
Short Lived: Short weak references looses the reference when GC comes into the picture
Long Lived: Long weak references retained in the memory even GC comes and reclaims the memory. so it means it's not been collected even the Finalizer of the method is called.
How can we create Weak References ?
WeakReference objWeakReference = new WeakReference(null);
How we can achieve this long lived weak References ?
WeakReference objWeakReference = new WeakReference(null, true);
So now you can easily see this if you want to create a long lived object then you need to set trackResurrection variable to true to stop tracking of a particular object.
So in this way you can create a weak reference. Lets understand this with one example -
So in the above code i am casting my weak reference to strong reference(as i know my strong references will not be collected by GC anyways) and checking whether it's null or not. If it is null or reclaims by GC then i am creating those strings again otherwise weak reference will fetch it from the memory.
You can also check whether your reference is alive or not.
objWeakReference.IsAlive; will return you bool whether the object is in memory or not.
Hope you enjoyed reading the article.
Happy Coding :)
Weak Reference - Even though application refers to the object, GC may collect those object or may not be. Those objects are weak referenced objects.
Confused ? Let me explain in brief -
We all know GC is a non deterministic background process so we do not know when it will be called. So only weak referenced objects may reside in the memory for the long time or may not be. So it's up to us whether we want our weak references in memory or not. Lets look over the below examples -
Weak references are of two types -
Short Lived: Short weak references looses the reference when GC comes into the picture
Long Lived: Long weak references retained in the memory even GC comes and reclaims the memory. so it means it's not been collected even the Finalizer of the method is called.
How can we create Weak References ?
WeakReference objWeakReference = new WeakReference(null);
How we can achieve this long lived weak References ?
WeakReference objWeakReference = new WeakReference(null, true);
So now you can easily see this if you want to create a long lived object then you need to set trackResurrection variable to true to stop tracking of a particular object.
So in this way you can create a weak reference. Lets understand this with one example -
So in the above code i am casting my weak reference to strong reference(as i know my strong references will not be collected by GC anyways) and checking whether it's null or not. If it is null or reclaims by GC then i am creating those strings again otherwise weak reference will fetch it from the memory.
You can also check whether your reference is alive or not.
objWeakReference.IsAlive; will return you bool whether the object is in memory or not.
Hope you enjoyed reading the article.
Happy Coding :)