To understand Garbage collection mechanism we must know about the various reference types for object because on that basis the GC reclaim the memory from the object
There are following 4 kinds of reference types in Java:
Strong references.
Weak references.
Soft references.
Phantom references.
Strong reference: It is most simple as we use it in day to day programming. if not differently specified then an object is a strong Reference Object
Ex String language="English";
A new string object will be created and a strong reference to it would be stored in the variable 'language';
If any object has a strong reference or it is reachable by any chain of strong reference then JVM will not garbage collect it to avoid any situation where it destroy the object which is currently being worked upon.
Weak reference: These are created using WeakReference class which is present in the java.lang.ref . Weak references are those which are not strong enough to hold the Garbage collection of its object.
As the weakObject is set to null and is eligible for GC, weakReference will not be able to stop GC from collecting the weakObject as the WeakReference is not a strong reference to it.
Now, suppose you start calling some method on weakReference object and it suddenly starts returning 'null' because weakObject has been garbage collected then it will cause a problem.
so better solution to this is using WeakHashMap which work same like HashMap except that the keys are referred using weak reference.
If a key is garbage collected then its entry is removed automatically. SO it will help to avoid null pointer as in above case
Soft Reference : These are created using SoftReference class in java.lang.ref, the soft reference are same like weak reference except that it is not as eager to throw object as weak reference is.
Weak reference would be collected on next GC run, immediately but soft reference can delay the garbage collection of the object.
Soft reachable object are generally retained as long as there is plentiful memory available. This makes them a good candidate to be used for cache.
However weak references can be used for meta data.
Phantom reference : These are the weakest references, calling get on this object will always return null.
When we construct phantom reference we must always pass in a RefrenceQueue object which would be helpful to see whether the phantom object has been garbage collected at a particular time or not.
WeakReferences are enqueued as soon as the object to which they point becomes weakly reachable however WeakReference can be resurrect by calling get() method
but get() method on PhantomReference always return null so these can not be resurrect.
There are following 4 kinds of reference types in Java:
Strong references.
Weak references.
Soft references.
Phantom references.
Strong reference: It is most simple as we use it in day to day programming. if not differently specified then an object is a strong Reference Object
Ex String language="English";
A new string object will be created and a strong reference to it would be stored in the variable 'language';
If any object has a strong reference or it is reachable by any chain of strong reference then JVM will not garbage collect it to avoid any situation where it destroy the object which is currently being worked upon.
Weak reference: These are created using WeakReference class which is present in the java.lang.ref . Weak references are those which are not strong enough to hold the Garbage collection of its object.
RefrenceStudy weakObject= new RefrenceStudy();// now the weakObject is eligible for Garbage collection and may be collected when next time GC runs
WeakReference<RefrenceStudy> weakReference = new WeakReference<RefrenceStudy>(weakObject);
weakObject=null;
As the weakObject is set to null and is eligible for GC, weakReference will not be able to stop GC from collecting the weakObject as the WeakReference is not a strong reference to it.
Now, suppose you start calling some method on weakReference object and it suddenly starts returning 'null' because weakObject has been garbage collected then it will cause a problem.
so better solution to this is using WeakHashMap which work same like HashMap except that the keys are referred using weak reference.
If a key is garbage collected then its entry is removed automatically. SO it will help to avoid null pointer as in above case
Soft Reference : These are created using SoftReference class in java.lang.ref, the soft reference are same like weak reference except that it is not as eager to throw object as weak reference is.
Weak reference would be collected on next GC run, immediately but soft reference can delay the garbage collection of the object.
Soft reachable object are generally retained as long as there is plentiful memory available. This makes them a good candidate to be used for cache.
However weak references can be used for meta data.
Phantom reference : These are the weakest references, calling get on this object will always return null.
When we construct phantom reference we must always pass in a RefrenceQueue object which would be helpful to see whether the phantom object has been garbage collected at a particular time or not.
WeakReferences are enqueued as soon as the object to which they point becomes weakly reachable however WeakReference can be resurrect by calling get() method
but get() method on PhantomReference always return null so these can not be resurrect.
Comments
Post a Comment