|
|
Re: Need clarification on GC process
|
Posted: Mar 17, 2005 1:50 AM
|
|
hi, see u need not explicitly make the objects to null se there are two thing reachable and nonreachable. reachable--->if the objects exist somewhere in the program(i.e ) they r used somewhere in the program then they would not be garbage collected
not reachable--->if u cant find the objects no where in the program i.e they are not used those objects will be garbage collected
ok u need not explicitly set to null if the object is not used its memory will automatically be released during garbage collection u can enforce garbage collection in the above way
public class Sample { public void finalize() { System.out.println("garbage collection"); } public int calBalance(String acctid){ int i = 0; AccountReposit acctobj = null; try { acctobj = new AccountReposit(); i = acctobj.getBal(acctid); System.gc(); System.out.println("check the object after garbage collection"+acctobj ); } catch(Exception e){ } /*finally { if(acctobj!=null) acctobj=null; }*/ return i; } }
--> see here finalize method of object has been overridden when u call System.gc() ,the garbage collection process takes place and the finalize method wiill be called.
see there are referencce classes called SoftReference,WeakReference,PhantomRefernce that might help fo garbage collection,these classes are used to hold object refernces u can get mopre details about it in java.lang.ref in javadocs
|
|