aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic


Ranch Hand

Joined: Sep 14, 2000
Posts: 43
At what point is the object anObj available for garbage collection.

01: public class Base{
03: private void test() {
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
14: static public void main(String[] a) {
15: new Base().test();
16: }
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
please help:

Joined: Sep 16, 2000
Posts: 21
The answer is
b) After line 8
Some how I feel that this makes the object reference eligible for garbage collection though when it will happen is not possible to note.

[This message has been edited by jag_nai (edited September 19, 2000).]
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
object referred by anobj becomes eligible for garbage collection after line #11.
Because, trim() method will create and return another string object (which will immediately become eligible for GC). Even though anObj is set to null at line # 9, locObj is still referring to it. So the object will become eligible for GC only at the end of its declaring context (in this case the if block).
It is sorta covered in the JavaRanch Style Guide.
subject: GC