• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collector!!!!!!!!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
following question is from mock exam -SARGA.com
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
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

answer given is -- >e
but whereever i think it should be c as when we set "anObj" to null it will be available for gc.
pl. tell me the right answer and correct me if wrong
thanks in adv.
sunil
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not available for GC after we set anObj to null because we still have this object's reference stored in locObj.
P.S. btw I'm not sure if strings from the string pool are available for GC ...
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at your code (I just added 2 lines for printing the object). Now I still see anObj still printing "sample", after line 11, meaning that it will not be Gced after line 11. So my answer is none of them.
public class Base{
private void test() {
if(true) {
String anObj = "sample";
String locObj = anObj;
anObj.trim();
anObj = null;
locObj.trim();
System.out.println(anObj);//null
System.out.println(locObj);//sample
}
}
static public void main(String[] a) {
new Base().test();
}
}
Albert

Originally posted by sunil kathuria:
following question is from mock exam -SARGA.com
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
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

answer given is -- >e
but whereever i think it should be c as when we set "anObj" to null it will be available for gc.
pl. tell me the right answer and correct me if wrong
thanks in adv.
sunil


 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Albert,
I think you are wrong. When a trim does not remove any spaces from the string then it returns the reference to the string itself. So at line 10 locObj is still referring to "sample". Since after line 11 (closing }) it goes out of scope it would be eligible for garbage collection.
please correct me if I am wrong.
regards

Originally posted by Albert Gray:
Look at your code (I just added 2 lines for printing the object). Now I still see anObj still printing "sample", after line 11, meaning that it will not be Gced after line 11. So my answer is none of them.
public class Base{
private void test() {
if(true) {
String anObj = "sample";
String locObj = anObj;
anObj.trim();
anObj = null;
locObj.trim();
System.out.println(anObj);//null
System.out.println(locObj);//sample
}
}
static public void main(String[] a) {
new Base().test();
}
}
Albert


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshul,
Have to agree with you. 'anObj' is declared in the 'if' block and as no reference to it was passed to any variable outside the block it would be eligible for gc when the 'if' completes.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic