Thursday, December 7, 2017

How To Make Object Eligible For Garbage Collector In Java


In the old language like c++, creation and destruction of the object is a responsibility of programmer only. Usually, programmer taking very much care while creating objects and his/her neglecting destruction of useless objects due to this neglecting at the certain point of time for the creation of new object sufficient memory may not be available and the entire program will be collapse due to the memory problem. But in java, a programmer is responsible only for the creation of objects and he is not responsible for the destruction of useless objects.


Some people provided one assistant which is always running in the background for the destruction of useless objects. Due to this assistant the chance of failure java program with memory problems very rare. This assistant is nothing but Garbage Collector. Hence, the main objective of the garbage collector is to destroy useless objects.


The Various ways to make object eligible for Garbage Collector
Although programmer is not answerable to destroy meaningless objects, it is ever a better programming habit to make an object eligible for garbage collector if it is no continue needed. An object is said to be eligible for the garbage collector if it doesn't contain any reference. The following are the various possible ways to make an object eligible for garbage collector


1. Nullifying the reference variable
If an object is no longer required then assign null to all its references, the automatically that object eligible for a garbage collector.
 
 
Exam. 


No comments:

Post a Comment