Originally posted by Geethakrishna Srihari:
1.Whether a static reference to an object if either reassigned or assigned to null, the object becomes eligible to garbage collection?
2.Whether a string if reassigned to another or assigned to null becomes eligible for garbage collection?
1) Yes the object will be eligible for collection if there are no live references to it.
2) A String object yes, but a String constant no. The garbage collector only deals with objects and thus anything in the string constant pool is not dealt with by the garbage collector
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
Both of these are untrue.
An object will become eligible for garbage collection when there are no strong references to it that can be rooted (e.g. exclusively circular strong references to an object make eligible garbage collection). A static member exists for the lifetime of the class loader that loaded it, therefore, if that class loader is ever eligible for collection, that is the time that the object referred to by a static reference becomes eligible.
String constants may certainly be garbage collected. To be honest, I'm tired of this little myth on this forum and explaining it away every time, but I've attempted an explanation here: http://qa.jtiger.org/GetQAndA.action?qids=68
Thanks and Regards, Amit Taneja
Originally posted by rajan singh:
1
2. String str1="garbage"
String str2 = new String("garbage");
str1=null;
str2=null;
after the execution of above code the string literal "garbage" is not elligible for garbage collection coz the String class maintains the static pool to store the referneces of literals.
String referenced by str2 will elligible for garbage collection.Since str2 in not pointing to string literal.
if i misunderstood the problem please let me know.
Thanks and Regards, Amit Taneja
Shubhada
I have 1 doubt.In java ,objects are automatically garbage collected.Then why we need to use System.gc() & finalize() methods???
Consider Paul's rocket mass heater. |